android - Recyclerview item点击波纹效果

标签 android android-layout android-fragments 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>

适配器模板如下所示

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

请给我解决方案

最佳答案

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>

关于android - Recyclerview item点击波纹效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38523680/

相关文章:

Android GraphicBufferAlloc::createGraphicBuffer(w=1232, h=800) 失败(内存不足)

android - 获取root设备上/data/data中的数据库文件

Android:用完大量内存?

方向更改时重新创建 Android View

android - 来自服务器的带有数字标记的谷歌地图

android - 如何在导航组件中打开 fragment 的多个实例?

android - NavigationView 项目的涟漪效应

android - 在启动时,我的 Activity 屏幕位置切断了一半的 UI

android - 使用 ActionBar 旋转 Android 的双 fragment

Android:ListView 推出它下面的所有其他内容