RecyclerView: Highlight selected item

Paul Woitaschek picture Paul Woitaschek · Oct 25, 2014 · Viewed 8.3k times · Source

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?

Answer

Paul Woitaschek picture Paul Woitaschek · Mar 17, 2015

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.