Emsal Emsal - 1 month ago 16
Android Question

Not all items are selected in recyclerview android

In my

OnBindViewHolder
I've added the following code:

if(checkAllItems) {
viewHolder.checked_for_deletion.setChecked(true);
} else {
viewHolder.checked_for_deletion.setChecked(false);
}


The problem is that
OnBindViewHolder
is only called for visible items + the cached items which is 2 by default. I need to select also the items that are not visible. I've looked around for solutions but couldn't find any that worked.

Answer

You cannot check all the items when binding the ViewHolder.

That is because only a certain number of ViewHolders are created at once (to fill your RecyclerView). These ViewHolders are then "recycled" as they pass out of the bounds of the RecyclerView. They are reused for future rows of content.

What you need to do is hold the state of the CheckBox in your data model. Then, you check whether the CheckBox needs to be checked with each call of onBindViewHolder.