Vaibhav Sharma
Vaibhav Sharma

Reputation: 2319

Checkedchange listener on a checkbox in a recyclerview

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

Answers (2)

OmerCohen1994
OmerCohen1994

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

RogueBaneling
RogueBaneling

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

Related Questions