Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ripple effect only working on first ListView item

Tags:

android

I have implemented the ripple effect introduced in Lollipop in a ListView. But it is only working for the first item of ListView. I've followed the answers of this question but I'm unable to set a ripple effect.

I have a ripple_background.xml in drawable-v21 folder:

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/background_dark">
    <item>
        <shape
            android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

The layout my ListView exists in is as following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/listSettings"
        android:listSelector="@android:color/transparent"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:divider="@color/price_item_divider"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="true"/>
</LinearLayout>

and the items populated inside ListView is as follows (settings_item_row.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:background="@drawable/ripple_background"
    android:padding="16dp" >

    <ImageView
        android:id="@+id/imageViewIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingRight="10dp" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageViewIcon"
        android:paddingRight="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

</RelativeLayout>

Thank you for your help!

like image 962
Basit Avatar asked Jan 21 '15 17:01

Basit


1 Answers

I faced same issue and found an easy solution to this problem. No need to set any background to any list item or ListView. Just set android:listSelector to your ripple drawable in ListView.

So ListView in my activity look like:

<ListView
        android:id="@+id/myListView"
        ...
        android:listSelector="@drawable/ripple" />

and my ripple drawable is defined as :

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/rippleColour">
    <item android:drawable="@color/backgroundColour"/>
</ripple>
like image 143
Pushkar Avatar answered Sep 24 '22 09:09

Pushkar