我刚创建了一个包含文本和复选框的回收站视图我认为我为此Recycler项创建了一个适配器 适配器类
package abtech.waiteriano.com.waitrer.adapters;
/**
* Created by dell on 7/13/2017.
*/
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import abtech.waiteriano.com.waitrer.R;
import cn.refactor.library.SmoothCheckBox;
public class ItemModifierAdapter extends RecyclerView.Adapter<ItemModifierAdapter.ItemModifierVierHolder> {
private Context mContext;
static ArrayList<ItemModifierModel> itemModifierModels;
static ArrayList<Boolean> positionArray;
public ItemModifierAdapter(Context context, ArrayList<ItemModifierModel> itemModifierModels) {
mContext = context;
this.itemModifierModels = itemModifierModels;
positionArray = new ArrayList<Boolean>(itemModifierModels.size());
for (int i = 0; i < itemModifierModels.size(); i++) {
positionArray.add(false);
}
}
public static class ItemModifierVierHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
Context c;
TextView itemName, modifierName;
SmoothCheckBox Iv1;
private SparseBooleanArray selectedItems;
RelativeLayout RV1;
private Context mContext;
ItemModifierVierHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
Iv1 = (SmoothCheckBox) itemView.findViewById(R.id.iv1);
modifierName = (TextView) itemView.findViewById(R.id.modifierItemNameTv);
RV1 = (RelativeLayout) itemView.findViewById(R.id.rv1);
//set OnclickListener
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Iv1.toggle();
Iv1.mTickDrawing = false;
Iv1.mDrewDistance = 0;
if (Iv1.isChecked()) {
// RV1.setTag(true);
// ` Iv1.setVisibility(View.VISIBLE);
// Iv1.setChecked(false);
positionArray.set(position, true);
Iv1.startCheckedAnimation();
} else {
// Iv1.setVisibility(View.INVISIBLE);
// Iv1.setChecked(true);
positionArray.set(position, false);
Iv1.startUnCheckedAnimation();
// RV1.setTag(null);
}
Toast.makeText(itemView.getContext(), "Position: " + itemModifierModels.get(Integer.parseInt(getAdapterPosition() + "")).getSer(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public ItemModifierVierHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_modifier, viewGroup, false);
ItemModifierVierHolder imvh = new ItemModifierVierHolder(v);
return imvh;
}
@Override
public void onBindViewHolder(ItemModifierVierHolder itemModifierVierHolder, final int position) {
for (int i = 0; i < itemModifierModels.get(position).modifierName.length; i++) {
if (i == 0)
itemModifierVierHolder.modifierName.setText(itemModifierModels.get(position).modifierName[i]);
else
itemModifierVierHolder.modifierName.setText(itemModifierVierHolder.modifierName.getText().toString() + "\n" + itemModifierModels.get(position).modifierName[i]);
}
// itemModifierVierHolder.modifierName.setText(itemModifierModels.get(position).modifierName[0]);
itemModifierVierHolder.RV1.setFocusable(false);
itemModifierVierHolder.Iv1.setChecked(positionArray.get(position));
}
@Override
public int getItemCount() {
return itemModifierModels.size();
}
}
并将此项添加到String []
中Modifiers = new ArrayList<>();
Modifiers.add(new ItemModifierModel(1,(new String []{"Roomy Roll","Kiri Roll","Roomy Roll","Eggs Roll","Modifier 5","Modifier 6"}) ));
Modifiers.add(new ItemModifierModel(16, (new String []{"Eggs Roll"})));
Modifiers.add(new ItemModifierModel(23,(new String []{"Kiri Roll"})));
Modifiers.add(new ItemModifierModel(43,(new String []{"Roomy Roll"})));
Modifiers.add(new ItemModifierModel(50,(new String []{"Roomy Roll"})));
Modifiers.add(new ItemModifierModel(56,(new String []{"Kiri Roll"})));
Modifiers.add(new ItemModifierModel(63,(new String []{"Kiri Roll"})));
Modifiers.add(new ItemModifierModel(73,(new String []{"Eggs Roll"})));
Modifiers.add(new ItemModifierModel(80,(new String []{"Eggs Roll"})));
这是我的模特
package abtech.waiteriano.com.waitrer.adapters;
/**
* Created by dell on 7/13/2017.
*/
public class ItemModifierModel {
String[] modifierName;
int ser;
ItemModifierModel(int ser, String[] modifierName) {
this.modifierName = modifierName;
this.ser = ser;
}
public String[] getModifierName() {
return modifierName;
}
public int getSer() {
return ser;
}
}
但它只显示了这样的1个文本 我需要的只是展示其他文本,我还需要这些文本显示在彼此之后和彼此之下 我应该为这些文本制作另一个适配器和模型吗?
答案 0 :(得分:1)
onBindViewHolder
你把这个
itemModifierVierHolder.modifierName.setText(itemModifierModels.get(position).modifierName[0]);
由于您通过执行modifierName[0]
显式设置单个修饰符,因此它显示单个条目。
如果要动态添加更多TextView,请使用其他布局包装modifierName
TextView。之后,您可以使用每个modifierName
项动态地向其添加TextView。
答案 1 :(得分:1)
请检查我的解决方案,希望它对您有所帮助:
package com.app.practiceapp;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] namesArray1 = new String[]{"Cupcake", "Donut", "Eclair", "Froyo"};
Model model1 = new Model();
model1.setNamesArray(namesArray1);
String[] namesArray2 = new String[]{"Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean"};
Model model2 = new Model();
model2.setNamesArray(namesArray2);
String[] namesArray3 = new String[]{"Kitkat", "Lollipop", "Marshmallow", "Nougat"};
Model model3 = new Model();
model3.setNamesArray(namesArray3);
String[] namesArray4 = new String[]{"Prashant", "Android", "Developer"};
Model model4 = new Model();
model4.setNamesArray(namesArray4);
ArrayList<Model> modelList = new ArrayList<>();
modelList.add(model1);
modelList.add(model2);
modelList.add(model3);
modelList.add(model4);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter(modelList));
}
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<Model> modelList;
MyAdapter(ArrayList<Model> myList) {
this.modelList = myList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview, parent, false));
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
for (int i = 0; i < modelList.get(position).getNamesArray().length; i++) {
TextView textView = new TextView(MainActivity.this);
textView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
textView.setLayoutParams(layoutParams);
textView.setPadding(10, 10, 10, 10);
textView.setBackgroundColor(ContextCompat.getColor(MainActivity.this, android.R.color.holo_blue_light));
textView.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.white));
textView.setText(modelList.get(position).namesArray[i]);
holder.itemContainerLayout.addView(textView);
}
}
@Override
public int getItemCount() {
return modelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private final LinearLayout itemContainerLayout;
public MyViewHolder(View itemView) {
super(itemView);
itemContainerLayout = (LinearLayout) itemView.findViewById(R.id.containerLayout);
}
}
}
class Model {
private String[] namesArray;
public void setNamesArray(String[] namesArray) {
this.namesArray = namesArray;
}
public String[] getNamesArray() {
return namesArray;
}
}
}
这是主要活动布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.practiceapp.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是Recycler View项目布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
</LinearLayout>
如果对您的要求有帮助,请告诉我。
答案 2 :(得分:0)
我有一个丑陋的解决方案,尝试使用ItemModifierVierHolder创建一个sub recyclerView,然后为每个子数据项添加一个checkBox + textView