Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView item not showing ripples/touch feedback when the item has a checkbox

I want the items in a RecyclerView to have touch feedback or ripples when pressed, but they seem to not be working, and I think it's because of the checkbox.

The ripple is only shown when long pressing, but a simple press won't show it.

May someone help me to fix it? Thanks in advance.

PD: I was using a ListView, and the item layout parent was a LinearLayout. The ripples were working fine. After moving to RecyclerView, the items ripples don't work. I tried with the LinearLayout again, but still not working.

Here's the layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/requestCard"
    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="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:maxHeight="64dp"
        android:maxWidth="64dp"
        android:padding="@dimen/lists_padding"
        android:src="@drawable/ic_launcher"
        tools:ignore="ContentDescription"/>

    <TextView
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/imgIcon"
        android:layout_toRightOf="@+id/imgIcon"
        android:ellipsize="end"
        android:maxLength="@integer/request_text_length"
        android:maxLines="1"
        android:padding="@dimen/lists_padding"
        android:textSize="@dimen/abc_text_size_large_material"
        tools:text="App Name"/>

    <CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:padding="@dimen/lists_padding"/>

</RelativeLayout>
like image 751
Jahir Fiquitiva Avatar asked Dec 10 '15 00:12

Jahir Fiquitiva


3 Answers

In your onClick method (assuming in your callback), make sure you do not call notifyDataSetChanged() after notifyItemChanged(position).

notifyDataSetChanged() will conflict with those default ripple effects.

new recyclerAdapter.ClickListener() {
    @Override
    public void onClick(int position) {

        ... awesome item onClick code ...

        notifyItemChanged(position);
        //notifyDataSetChanged(); <//--- Causes the no ripple bug
    }
};
like image 125
Codeversed Avatar answered Nov 16 '22 00:11

Codeversed


You use android:background="?android:attr/selectableItemBackground" but you should use android:foreground="?android:attr/selectableItemBackground"

like image 21
curioushikhov Avatar answered Nov 16 '22 00:11

curioushikhov


I think you are looking for ripple effect.I can give you basic idea, Just create one xml file (ripple.xml) in drawable & write this code inside

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white"
        >

    </item>
</ripple>

& just write two line in your another xml file in which you have declared recyclerview elements.

android:background="@drawable/ripple"
android:clickable="true"

Like in my case i am having only TextView and in parent tag i am declaring these two lines

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ripple"
    android:clickable="true"
   >

<TextView
    android:id="@+id/textView_Username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="@color/colorPrimary"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    />
</LinearLayout>

Finally the result:enter image description here

See this video

https://www.youtube.com/watch?v=qvVkDb7DqCk

like image 38
Däñish Shärmà Avatar answered Nov 15 '22 22:11

Däñish Shärmà