Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Selector in RecyclerView Item

I'm using the RecyclerView like below:

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="320dp"
    android:layout_height="match_parent"/>

and my list item:

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_medium_high">
    <com.basf.suvinil.crie.ui.common.widget.CircleView
        android:id="@+id/circle"
        android:layout_width="22dp"
        android:layout_height="22dp"/>
    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="57.5dp"/>
</LinearLayout>

see in detail this part android:background="@drawable/selector_medium_high" it's a normal selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background_high" android:state_activated="true"/>
    <item android:drawable="@color/background_high" android:state_pressed="true"/>
    <item android:drawable="@color/background_high" android:state_checked="true"/>
    <item android:drawable="@color/background_high" android:state_focused="true"/>
    <item android:drawable="@color/background_medium"/>
</selector>

but when I run this code, i have no changes in background color when I touch the row....

like image 256
ademar111190 Avatar asked Aug 19 '14 20:08

ademar111190


People also ask

How to change the background color of selected item in recyclerview?

Add click listener for item view in .onBindViewHolder () of your RecyclerView's adapter. get currently selected position and change color by .setBackground () for previously selected and current item Show activity on this post. Most Simpler Way From My Side is to Add a variable in adapterPage as last Clicked Position.

How to show activity of selected item in recyclerview?

Add the property into your xml (where you declare the RecyclerView): Show activity on this post. Add click listener for item view in .onBindViewHolder () of your RecyclerView's adapter. get currently selected position and change color by .setBackground () for previously selected and current item Show activity on this post.

How to set the random background for recyclerview in Android?

Using recycler view we can show grids and list of items. This example demonstrates How to set the random background for Recyclerview by creating a beautiful student records app that displays student name with age. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to properly highlight the selected item on Android recyclerview?

This example demonstrates how do I properly highlight the selected item on android RecyclerView. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Add the following dependency in the build.gradle (Module: app)


4 Answers

Set clickable, focusable, focusableInTouchMode to true in all elements of RecyclerView "list".

like image 66
Ahmed Ghonim Avatar answered Oct 08 '22 07:10

Ahmed Ghonim


Add :

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

in item.xml

like image 31
100rbh Avatar answered Oct 08 '22 08:10

100rbh


If nothing of this works for you, like it didn't for me, use this code:

android:foreground="?android:attr/selectableItemBackground"

The trick is in android:foreground attribute...

like image 17
NixSam Avatar answered Oct 08 '22 08:10

NixSam


Adding android:background="?android:attr/selectableItemBackground" to my_list_item.xml's root layout seems to work for me (assuming you want the default selection colour).

Also make sure the root layout's android:layout_width is match_parent rather than wrap_content to ensure that the whole row is selectable.

like image 13
Lawrr Avatar answered Oct 08 '22 06:10

Lawrr