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

前端 未结 3 1454
慢半拍i
慢半拍i 2021-01-12 19:11

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 ri

相关标签:
3条回答
  • 2021-01-12 20:04

    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:

    See this video

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

    0 讨论(0)
  • 2021-01-12 20:07

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

    0 讨论(0)
  • 2021-01-12 20:10

    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
        }
    };
    
    0 讨论(0)
提交回复
热议问题