Emsal Emsal - 5 months ago 55
Android Question

Not all items are selected in recyclerview android

In my

I've added the following code:

if(checkAllItems) {
} else {

The problem is that
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.


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.