我刚创建了一个包含文本和复选框的 Recycler View。我假设为此 Recycler Item 创建了一个适配器类。
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;
}
}
但是它只显示了一个文本,如下所示:
![enter image description here](https://istack.dev59.com/0nIyn.webp)
我想创建像这样的东西,使用相同的文本视图但不同的名称来重复文本。
![enter image description here](https://istack.dev59.com/jbZfD.webp)