Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView: Highlight selected item

I have implemented a RecyclerView and I have set it up to use CAB. But how can I highlight the selected items? If a certain position I checked I stored in a SparseBooleanArray. My first thought was to store the specific View containg all the elements in my ViewHolder and then in onBindViewHolder set the background somehow to: ?android:attr/activatedBackgroundIndicator But how can I do that? Is that a useful approach?

like image 675
Paul Woitaschek Avatar asked Oct 25 '14 15:10

Paul Woitaschek


2 Answers

I finally solved this by simply adding some minor things:

First of all, the items of the RecyclerView have to use this as background:

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

Then for the RecyclerView simply call: setSelected(true); on the indiviual views.

like image 170
Paul Woitaschek Avatar answered Oct 01 '22 04:10

Paul Woitaschek


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

If you don't need to update the view itself, I would suggest using an item decorator.

like image 36
yigit Avatar answered Oct 01 '22 03:10

yigit