RecyclerView: select the selected item

I implemented RecyclerView and I installed it to use CAB . But how can I highlight selected items? If a specific position that I checked, I saved in a SparseBooleanArray . My first thought was to save a specific View containing all the elements in my ViewHolder , and then set the background to something like onBindViewHolder ?android:attr/activatedBackgroundIndicator But how can I do this? Is this a useful approach?

+7
android android-recyclerview cab
source share
2 answers

I finally solved this by simply adding some minor things:

First of all, RecyclerView elements should use this as a background:

 android:background="?android:attr/activatedBackgroundIndicator" 

Then for RecyclerView just call: setSelected (true); on individual performances.

+3
source share

If you want to change the view itself, you need to send adapter.notifyItemChanged (position), and in response the recycler view will call the onBind method, where you can set the background.

If you do not need to update the view, I would suggest using the item decorator.

+1
source share

All Articles