Recyclerview项目点击涟漪效应

时间:2016-07-22 10:06:00

标签: android android-layout android-fragments android-recyclerview recycler-adapter

我正在尝试将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>

和适配器模板如下所示

<?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>

请给我解决方案

2 个答案:

答案 0 :(得分:41)

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>

答案 1 :(得分:3)

在您的recyclerView项目父项中,添加

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

如下所示:

<?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:background="?android: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>
相关问题