android - 将波纹效果添加到 RecyclerView 项目

标签 android android-animation android-recyclerview

我正在尝试将波纹效果添加到 RecyclerView 的项目。我在网上看了一下,但找不到我需要的东西。我认为它必须是自定义效果。我已尝试将 android:background 属性设置为 RecyclerView 本身并将其设置为“?android:selectableItemBackground”,但它不起作用。:

    <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:clickable="true"
    android:background="?android:selectableItemBackground"
    android:id="@+id/recyclerView"
    android:layout_below="@+id/tool_bar"/>

这是我尝试添加效果的 RecyclerView:

enter image description here

最佳答案

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

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

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

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

结果:

enter image description here

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

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

关于android - 将波纹效果添加到 RecyclerView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931889/

相关文章:

android - 无法从 RecyclerView.OnScrollListener 调用 notifyItemInserted()

java - 如何将值从处理程序传递到方法

java - 如何在 ROOTED 设备上录制 VOIP 电话?

android - 调试 Android 应用程序

android - 使用删除与隐藏时不调用自定义动画

java - getAdapterPosition 返回 NO_POSITION,返回实际值(适配器位置 - 1)

java - 这是 Kotlin 中的数据构造 'or' Enum 类型吗?

android - Canvas 上字符串的简单淡出动画,android

android - 动画 android Gridview 项目

java - 如何仅在按下按钮时才重新加载 recyclerview?