Recyclerview项目点击涟漪效应

Kar*_*nga 15 android android-layout android-fragments recycler-adapter android-recyclerview

我正在尝试将Ripple效果添加到RecyclerView项目中.我在网上看了一下,但找不到我需要的东西.我已经尝试了android:background属性并将RecyclerView其设置为"?android:selectableItemBackground"但它没有工作:

我的父布局是这样的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/dailyTaskList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:scrollbars="vertical" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

和适配器模板如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:id="@+id/txtTitle"
                style="@style/kaho_panel_sub_heading_textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txtDate"
                style="@style/kaho_content_small_textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

请给我解决方案

Joa*_*Ley 45

添加android:background="?attr/selectableItemBackground"到项目布局的最顶层父项应该可以解决问题.但是在某些情况下它仍然会错过动画,添加android:clickable="true"它就可以了.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/txtTitle"
            style="@style/kaho_panel_sub_heading_textview_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtDate"
            style="@style/kaho_content_small_textview_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 因为`ConstraintLayout`不起作用. (4认同)