【问题标题】:Get ripple effect in RecyclerView with items containing another background color使用包含另一种背景颜色的项目在 RecyclerView 中获取波纹效果
【发布时间】:2023-04-05 09:55:01
【问题描述】:

我需要在我的 RecyclerView 元素中添加一个简单的涟漪效果,这些元素设置了背景颜色。由于他们已经设置了背景颜色(蓝色),我无法将背景设置为 drawable/ripple.xml:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/color_view_pressed"> <!-- ripple color -->

<item android:drawable="@android:color/white"/>
<!-- normal color -->

所以我把我的ripple.xml改成:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/color_view_pressed"> <!-- ripple color -->

<item android:drawable="@android:color/blue"/>
<!-- normal color -->

还是不行。 :( 编辑: 显然,波纹颜色隐藏在背景颜色后面,因此几乎看不到。但是人们可以在背景中看到它的一丝色彩(如果我将 color_view_pressed 设置为深红色)。 有没有什么办法可以做到这一点?我已经尝试了所有可能的解决方案。 不知何故,它与 ListView 完美配合。不过我不明白。

【问题讨论】:

    标签: android android-recyclerview ripple rippledrawable


    【解决方案1】:

    在用于项目的布局的根 ViewGroup 中添加前景属性而不是背景(您已经设置):

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

    并设置这些属性:

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

    这对我有用

    【讨论】:

    • 所以我将根 ViewGroup 的前景(即用于单个 RecyclerView 项目的布局)设置为此,并将背景设置为所需的颜色。但它仍然不起作用。背景颜色正确。但涟漪效应并没有出现。 :(
    • @Sid 使用此功能时,请删除您对ripple.xml 的尝试,不再需要
    • 是的,我已经删除了我对ripple.xml 的尝试。
    • 我已将背景颜色设置为所需的蓝色。
    • @Sid 检查更新后的帖子,这正是我的工作方式。希望你解决
    【解决方案2】:

    您可以有 2 个 ViewGroups 有一个背景 - 顶部的涟漪,它的孩子有蓝色背景的整个布局。

    【讨论】:

    • 这也不起作用。得到了背景色,但根本看不到波纹效果。 :(
    猜你喜欢
    • 2015-04-22
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2017-02-21
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多