Skip to content Skip to sidebar Skip to footer

Recyclerview.adapter Resets Radio-buttons

This is my problem: https://youtu.be/k-N5uthYhYw and this is my onBindViewHolder() method. // Replace the contents of a view (invoked by the layout manager) @Override public void

Solution 1:

I know that this was answered already but if some of you are still looking for an easier answer and your application does not rely on the RecyclerView view recycling feature much (for example if you have a fixed size list of items...) you can always set your recycler view cache view size. That way it would not recycler your views hence it would not recycler the views and you will avoid copy selected values to another views...

yourRecyclerView..setItemViewCacheSize(yourItemList.size());

Solution 2:

Save the checked / unchecked status of the radio button (you should use checkbox instead if you want to allow the user to select multiple items) to your model (i.e. your items in the list should have a field for this) when the onClick event happens. When you bind the ViewHolder, make sure you set checkbox's value to whatever you saved in your model.

Solution 3:

It's happen because of the Recycling mechanism

(PS: its the same for the ListView or RecyclerView).

To fix that:

  • 1) Add a booelan variable to your model to save the state of the RadioButton

  • 2) Update your RadioButton state in onBindViewHolder() method from this boolean in the model.

  • 3) Add setOnCheckedChangeListener() to your RadioButton to listen to his state (checked/unchecked) and to update the boolean in your model when the state changes.

Post a Comment for "Recyclerview.adapter Resets Radio-buttons"