且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向 RecyclerView 项目添加波纹效果

更新时间:2023-11-17 13:58:16

我想通了.我唯一要做的就是添加这个属性:

I figured out. The only thing that I had to do is to add this attribute:

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

到我的 RecyclerView 适配器这样膨胀的布局的根元素:

to the root element of the layout that my RecyclerView adapter inflates like that:

<RelativeLayout 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:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:background="?android:attr/selectableItemBackground"
    tools:background="@drawable/bg_gradient">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:id="@+id/shoppingListItem"
        android:hint="@string/enter_item_hint"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/shopping_list_item_checkbox_label"
        android:id="@+id/shoppingListCheckBox"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:checked="false"/>

</RelativeLayout>

结果:

如果您仍然看不到波纹效果,请将这些行也添加到布局的根元素中.

If you are still not able to see ripple effect, add these lines also to the root element of the layout.

android:clickable="true"
android:focusable="true"