向 RecyclerView 项目添加波纹效果 [英] Adding Ripple Effect to RecyclerView item

查看:31
本文介绍了向 RecyclerView 项目添加波纹效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add Ripple Effect to RecyclerView's item. I had a look online, but could not find what I need. I assume it has to be a custom effect. I have tried android:background attribute to the RecyclerView itself and set it to "?android:selectableItemBackground" but it did not work.:

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

This is the RecyclerView that I am trying to add the effect to:

推荐答案

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

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"

这篇关于向 RecyclerView 项目添加波纹效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆