How to highlight selected item in RecyclerView

Just add this below line in bindView

holder.itemView.setBackgroundColor(Color.parseColor("#000000"));

will work for you.

if you want to highlight a selected item just do like below

set it global

int selectedPosition=-1;

inside onBindViewHolder-

public void onBindViewHolder(FiltersAdapter.ViewHolder holder, int position) {
if(selectedPosition==position)
  holder.itemView.setBackgroundColor(Color.parseColor("#000000"));
else
  holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition=position;
                notifyDataSetChanged();

            }
        });
}

===========================================================

Above code works fine as per old school methodology but here is the updated version which might help you:

Kotlin-

If you want to highlight a selected item just follow:

var selectedPosition = -1; //make it global

inside onBindViewHolder-

override fun onBindViewHolder(holder: FiltersAdapter.ViewHolder, position: Int) {
            if (selectedPosition == position)
                holder.itemView.setBackgroundColor(Color.parseColor("#000000"))
            else
                holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"))
        }

And create inner class ViewHolder which implements View.OnClickListener and override the onClick function.

override fun onClick(v: View) {
      when (v.id) {
           R.id.parent-> {
               selectedPosition = adapterPosition;
                        notifyDataSetChanged();
               }
           }
       }

Cheers.


May be just use:

android:background="?attr/selectableItemBackground"

for root element at item xml?


Use background selector and set that in android:background property in the layout xml for the recyclerview item

background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:state_selected="true">
        <shape>
            <solid android:color="@color/lightPrimaryColor" />
        </shape>
    </item>

    <item android:state_selected="false">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</selector>

recyclerview_item.xml (background_selector is set in android:background property)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_selector"
    android:orientation="vertical"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp">

And then the place where you get the click event you can set it as selected with the View function

view.setSelected(true)

You would have to implement the logic for when you want to unselect/select the item by storing the position of selected items