1

I am using the following xml drawable as the background of my recyclerview list item.

touch_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true"
        android:drawable="@color/green_text_500"/>
    <!-- Default, "just hangin' out" state. The base state also
         implements the ripple effect. -->
    <item android:drawable="@drawable/touch_selector_base" />
</selector>

touch_selector_base.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/light_grey">
    <item android:id="@android:id/mask" android:drawable="@color/light_grey" />
    <item android:drawable="@color/dark_grey"/>
</ripple>

In the list item I am using the touch_selector.xml as follows in my list_item_quote.xml

<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="86dp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/touch_selector"
    >
    // layout
</LinearLayout>

SIMILARLY I have another xml drawable touch_selector_dark.xml and touch_selector_base_dark.xml

In my recycler-view-adapter I alternate between these two drawables based on the index. For even indexes I use the darker drawable and for the odd index I use the lighter background to produce an alternating effect. But the issue is that the ripple effect is not working.

Here are the colors:

light_grey = #5b5b5b

dark_grey = #212121

green_text_500 = #37863a

Cœur
  • 37,241
  • 25
  • 195
  • 267
Muddassir Ahmed
  • 518
  • 4
  • 19
  • try this android:background="?attr/selectableItemBackgroundBorderless" for your adapter parent layout backround – PLP Nov 14 '16 at 12:04
  • Try the solution here http://stackoverflow.com/questions/28636377/ripple-effect-over-a-recyclerview-item-containing-imageview – Raghavendra Nov 14 '16 at 12:05

3 Answers3

1

use this line in your recycleview list layout

android:background="?android:attr/selectableItemBackground"
Vijay Chaudhary
  • 228
  • 2
  • 12
1

try this worked for me

android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
ali zarei
  • 1,212
  • 11
  • 17
0

I think your ripple file is not valid, you can check this:

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

You can use android:foreground="@drawable/touch_selector" to display the ripple effect and also use android:background="" to set any other drawable background.

MrLeblond
  • 1,005
  • 9
  • 26