在我的应用程序中,我使用了RecyclerView,包含文本视图和复选框,但是当选中一个复选框时,还会选择多个复选框 ,我发现问题类似于网站中的这种情况,我实际上试图实现它们来解决我的问题,但我不能,因为代码存在一些差异,这里是:
类RecyclerView_dAdapter扩展了RecyclerView.Adapter {
private List<List_Item> list_items;
private List<List_Item> favorite_itemes = new ArrayList<>();
private Context context;
RecyclerView_dAdapter(List<List_Item> list_Item, Context context) {
list_items = list_Item;
this.context = context;
}
@Override
public RecyclerView_dAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final RecyclerView_dAdapter.ViewHolder holder, final int position) {
List_Item current = list_items.get(position);
final String name = current.getName();
holder.TextName.setText(name);
holder.TextName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Show_Team_Activity.class);
intent.putExtra("TeamName", name);
context.startActivity(intent);
}
});
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
CheckBox chk = (CheckBox) v;
if (chk.isChecked()) {
favorite_itemes.add(list_items.get(pos));
chk.setChecked(true);
} else if (!chk.isChecked()) {
favorite_itemes.remove(list_items.get(pos));
chk.setChecked(false);
}
}
});
}
@Override
public int getItemCount() {
return list_items.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView TextName;
private CheckBox favorite_checkBox;
ItemClickListener itemClickListener;
ViewHolder(View view) {
super(view);
TextName = (TextView) view.findViewById(R.id.textView_rowitem);
favorite_checkBox = (CheckBox) view.findViewById(R.id.favorite_checkBox);
favorite_checkBox.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener ic) {
this.itemClickListener = ic;
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v, getLayoutPosition());
}
}
}
答案 0 :(得分:0)
这是因为RecyclerView回收了视图。在onBindViewHolder方法中添加这段代码:
holder.favorite_checkBox.setChecked(favorite_itemes.contains(current));
答案 1 :(得分:0)
您需要保持复选框状态。
@Override
public void onBindViewHolder(final RecyclerView_dAdapter.ViewHolder holder, final int position) {
List_Item current = list_items.get(position);
final String name = current.getName();
holder.TextName.setText(name);
holder.TextName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Show_Team_Activity.class);
intent.putExtra("TeamName", name);
context.startActivity(intent);
}
});
//set check boxes state
holder.favorite_checkBox.setChecked(favorite_itemes.contains(current))
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
CheckBox chk = (CheckBox) v;
if (chk.isChecked()) {
favorite_itemes.add(list_items.get(pos));
//chk.setChecked(true);
} else if (!chk.isChecked()) {
favorite_itemes.remove(list_items.get(pos));
//chk.setChecked(false);
}
//notify changes
notifyItemChanged(position);
}
});
}