public class SubcategoryAdapter extends RecyclerView.Adapter<SubcategoryAdapter.MyViewHolder> {
Context mContext;
private List<SubcategoryData> firstList;
boolean bind;
private ArrayList<SubcategoryData> subcategory_checked;
private List<Boolean> isCheckedArray;
public static class MyViewHolder extends RecyclerView.ViewHolder {
int subcategoryId;
int status;
@BindView(R.id.tv_subCategoryName)
TextView name;
@BindView(R.id.cb_chooseSub)
CheckBox chooseSub;
public MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
public SubcategoryAdapter(List<SubcategoryData> firstList, Context mContext) {
this.firstList = firstList;
subcategory_checked = new ArrayList<>(firstList.size());
for (int i = 0; i < firstList.size(); i++)
isCheckedArray = new ArrayList<Boolean>(firstList.size());
for (int i = 0; i < firstList.size(); i++) {
isCheckedArray.add(false);
}
this.mContext = mContext;
}
@Override
public SubcategoryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_subcategory, parent, false);
return new SubcategoryAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final SubcategoryAdapter.MyViewHolder holder, final int position) {
bind = true;
final SubcategoryData listq = firstList.get(position);
if (listq == null) {
return;
}
holder.name.setText(listq.getTitle());
holder.subcategoryId = listq.getId();
holder.chooseSub.setChecked(isCheckedArray.get(position));
holder.chooseSub.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (!bind) {
isCheckedArray.set(position, isChecked);
if (isChecked) {
subcategory_checked.add(position, listq);
SubcategoryData subcategoryData = new SubcategoryData();
subcategoryData.setId(holder.subcategoryId);
subcategoryData.setTitle(holder.name.getText().toString());
notifyDataSetChanged();
} else {
subcategory_checked.remove(position);
}
}
}
});
bind = false;
}
public ArrayList<SubcategoryData> getSubsIDsArray() {
return subcategory_checked;
}
@Override
public int getItemCount() {
return firstList.size();
}
}
我想在我的recyclerview中保存已检查的项目&gt;&gt;但我的问题是,当检查项目位置0必须在&gt;之后检查positiom 1如果uchecked位置0也必须在&gt;&gt;之后取消选中位置1如果在0应用程序崩溃后检查位置3 ..如果在检查1和2应用程序错误后检查位置3错误无效索引1,大小为0。