Reputation: 2319
If I have a checkbox in a recyclerview and also have a checked change listener to listen to its events, I find that some other viewholders too get a check even though I didn't check them!!
Is it because the recyclerview reuses its viewholders? How should I overcome this problem?
Upvotes: 2
Views: 703
Reputation: 184
You should override the onViewRecycled function in recyclerView and there set the checkbox checkChangeListener to null, set checked to false and call the super method. worked for me :)
Upvotes: 1
Reputation: 4471
Yes, the recycler view reuses its viewholders. In fact, that is the main purpose of the recycler view, and it allows for much faster scrolling as it is expensive to inflate a view and call findViewById
for each view instance.
You can overcome this problem by manually setting all the values in onBindViewHolder
. In your case, this would mean calling checkBox.setChecked(false)
to put the checkbox back to its default state.
Upvotes: 2