I'm trying to figure out how to achieve the same effect of
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
in a RecyclerView implementation. Please help.
These required methods are as follows: onCreateViewHolder(ViewGroup parent, int viewType) onBindViewHolder(RecyclerView. ViewHolder holder, int position)
There is no built-in support for a "choice mode" structure with RecyclerView
. Your options are to either roll it yourself or use a third-party library that offers it. The DynamicRecyclerView library offers choice modes, but I have not tried it.
This sample app demonstrates implementing it yourself, in this case using the activated state to indicate which is the current choice. The overall pattern is:
Have your RecyclerView.ViewHolder
detect a UI operation that indicates a choice (click on a row? click on a RadioButton
in the row? etc.).
Keep track of the selection at the level of your RecyclerView.Adapter
. In my case, a ChoiceCapableAdapter
handles that, in conjunction with a SingleChoiceMode
class that implements a ChoiceMode
strategy.
When a choice is made, update the newly-chosen row to reflect the choice and update the previously-chosen row to reflect that it is no longer chosen. findViewHolderForPosition()
on RecyclerView
can help here -- if you track the position
of the last choice, findViewHolderForPosition()
can give you the ViewHolder
for that choice, so you can "un-choose" it.
Keep track of the choice across configuration changes, by putting it in the saved instance state of the activity or fragment that is managing the RecyclerView
.
I've created a library for this kind of choice mode applied to the RecyclerView, maybe it can help:
This library has been created to help the integration of a multi-choice selection to the RecyclerView
The integration with Gradle is very easy, you just need the jcenter repository and the library:
repositories { jcenter() } ... dependencies { compile 'com.davidecirillo.multichoicerecyclerview:multichoicerecyclerview:1.0.1' }
Add the MultiChoiceRecyclerView to your xml file
<com.davidecirillo.multichoicesample.MultiChoiceRecyclerView android:id="@+id/multiChoiceRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" />
Instanciate you object and connect the view
MultiChoiceRecyclerView mMultiChoiceRecyclerView = (MultiChoiceRecyclerView) findViewById(R.id.multiChoiceRecyclerView);
Extend you adapter to the MultiChoiceAdapter and add it to the RecyclerView as per normal usage
public class MyAdapter extends MultiChoiceAdapter<MyViewHolder> { public MyAdapter(ArrayList<String> stringList, Context context) { this.mList = stringList; this.mContext = context; } ... } MyAdapter myAdapter = new MyAdapter(mList, getApplicationContext()); mMultiChoiceRecyclerView.setAdapter(myAdapter);
For more information and customisations: https://github.com/dvdciri/MultiChoiceRecyclerView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With