包含 ImageView 的 RecyclerView 项目上的波纹效果 [英] Ripple effect over a RecyclerView item containing ImageView

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

问题描述

我有一个 RecyclerView 可以扩展以下网格项:

I have a RecyclerView that expands the following grid item :

<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:layout_marginLeft="@dimen/children_tile_border"
    android:layout_marginTop="@dimen/children_tile_border"
    android:clickable="true"
    android:focusable="true"
    android:background="?selectableItemBackground"
    tools:ignore="RtlHardcoded">

        <com.example.app.ui.widget.SquareImageView
            android:id="@+id/child_picture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:layout_alignParentBottom="true"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="@color/tile_text_background"
            android:gravity="center_vertical"
            android:layout_alignParentBottom="true">

            ..............
        </LinearLayout>
</RelativeLayout>

但是除非我将 SquareImageView's 的可见性设置为不可见,否则不会出现涟漪效应.看起来涟漪效应是在 SquareImageView 下方绘制的.我怎样才能让它在 SquareImageView 上绘制?

But the ripple effect doesn't appear unless I set the SquareImageView's visibility to invisible. It seems the ripple effect is drawn below the SquareImageView. How can I can I make it drawn over the SquareImageView?

推荐答案

将以下代码添加到您的父布局

add below code to your parent layout

android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" 

如果你想要自定义涟漪效果,在你的 drawable-v21 中添加这个ripple_custom.xml

if you want custom ripple effect add this ripple_custom.xml in your drawable-v21

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorHighlight">

    <item
        android:id="@android:id/mask"
        android:drawable="@color/windowBackground" />
</ripple>

为了支持旧版本在drawable中添加ripple_custom.xml

to support older version add ripple_custom.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorHighlight" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</selector>

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

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