我有一個遊標,用於檢索成分列表並填充適配器,然後將其分配給回收站查看複選框。我得到的問題是,當我檢查一組複選框並向下滾動時,從頂部選擇的那些,取消選擇,反之亦然,或者在某些情況下,如果我從頂部選擇三個,從底部選擇三個得到選擇。我需要確保,最初沒有選擇任何東西,即使我上下滾動,所選擇的應該保持選擇狀態並需要跟蹤。複選框在滾動時在recyclerView中未被選中
下面是可以改變的列表。
這裏是我的代碼:
class RecipeDetailCursorAdapter extends CursorRecyclerViewAdapter<RecyclerView.ViewHolder> {
private int mBrownColor;
private int mGreenColor;
private int mCheckItems;
private ItemsCheckedCallback mCheckedCallback;
RecipeDetailCursorAdapter(Context context, Cursor cursor, ItemsCheckedCallback callback) {
super(context, cursor);
mCheckedCallback = callback;
mBrownColor = ContextCompat.getColor(mContext, R.color.colorTextBrown);
mGreenColor = ContextCompat.getColor(mContext, R.color.colorTextGreen);
mCheckItems = 0;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor) {
String taskName = cursor.getString(cursor.getColumnIndexOrThrow(MascotHelper.RecipeTask.NAME));
((RecipeTaskHolder) viewHolder).bindView(taskName);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recipe_task_item, parent, false);
return new RecipeTaskHolder(v);
}
private class RecipeTaskHolder extends RecyclerView.ViewHolder
implements SmoothCheckBox.OnCheckedChangeListener {
SmoothCheckBox taskBox;
TextView recipeComponentName;
RecipeTaskHolder(View v) {
super(v);
taskBox = (SmoothCheckBox) v.findViewById(R.id.recipe_task_box);
recipeComponentName = (TextView) v.findViewById(R.id.recipe_component_name);
taskBox.setOnCheckedChangeListener(this);
}
void bindView(String task) {
recipeComponentName.setText(task);
}
@Override
public void onCheckedChanged(SmoothCheckBox smoothCheckBox, boolean b) {
if (smoothCheckBox.isChecked()) {
recipeComponentName.setTextColor(mBrownColor);
mCheckItems++;
} else {
recipeComponentName.setTextColor(mGreenColor);
mCheckItems--;
}
if (mCheckItems == getItemCount()) {
//Toast.makeText(mContext, "All items are checked", Toast.LENGTH_SHORT).show();
mCheckedCallback.onAllItemChecked(true);
} else {
mCheckedCallback.onAllItemChecked(false);
}
}
}
interface ItemsCheckedCallback {
void onAllItemChecked(boolean status);
}
}
任何意見或建議,將不勝感激。
是的,對不起,我已經把不同版本的代碼,我現在編輯它。 – HShbib
如果您有解決方案,請將我的解決方案標記爲經過驗證的謝謝。 –
使用我的代碼來解決檢查和取消選中問題 –