I have a dialog which takes in user input through a list with checkboxes, whose layout is a RecyclerView
. But when I select a CheckBox
in the list, another CheckBox
further down in the list also gets checked, but which I didn't do. These images will help illustrate my point.
Here, I've only selected Calendar and Camera:
but further down in the list, Google and Maps also get selected which I didn't select.
My code for bindActivity
is:
public void bindActivity(ResolveInfo resolveInfo)
{
mResolveInfo = resolveInfo;
PackageManager pm = getActivity().getPackageManager();
String appName = mResolveInfo.loadLabel(pm).toString();
mAppImageView.setImageDrawable(resolveInfo.loadIcon(pm));
mAppTextView.setText(appName);
}
If I add mAppCheckBox.setChecked(false)
in bindActivity
, then when i go further down in the list and the RecyclerView
'recycles' the list, and then I go up, my earlier selection becomes unselected.
I would love any suggestions on how to get rid of the 'sticky' checkbox.
You see that behavior, because
ViewHolder
s are being reused. Once you scroll down, theViewHolder
of the upper items (that are not visible now) will be reused for the new items that are going to be displayed. ThisViewHolder
had aCheckBox
that was checked previously and nobody had explicitly said, that it should be unchecked.You should take care of resetting the state correctly. You can save checked items positions in an array and later in
onBindViewHolder()
set the selected state ofCheckBox
appropriately:Or you can refrain from "reinventing a wheel" and use
MultiViewAdapter
that is shipping with checkboxes feature: single, single or none, multiple modes.a) It doesn't look like you are setting the checked state of the checkbox when you are binding the view. Keep track of the items that have been selected, and then set the checked state when you doing view binding.
b) I'm not exactly sure what you mean, but from what I gather, the dialog documenation might be what you are looking for when communicating with the parent.