RecyclerView中没有涟漪效果

3

我正在使用RecyclerView来展示引用列表Fragment。我想在列表项中使用涟漪效果,但它不起作用。我的RecyclerView如下所示:

<android.support.v7.widget.RecyclerView
        android:id="@+id/listQuoteView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/BackColor"
        android:layout_above="@+id/startAppBanner"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

而列表项 XML 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.CardView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        app:cardCornerRadius="6dp"
        app:cardElevation="4dp">

        <LinearLayout
            android:id="@+id/quoteActionView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/TextColor"
            android:clickable="true"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageAuthorView"
                android:layout_width="@dimen/imageAuthorView"
                android:layout_height="@dimen/imageAuthorView"
                android:visibility="visible" />



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textQuote"
                    android:ellipsize="end"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:paddingRight="@dimen/list_text_margin"
                    android:paddingLeft="@dimen/list_text_margin"
                    android:gravity="center_vertical"
                    android:layout_weight="2"
                    android:maxLines="2"
                    android:layout_margin="2dp"
                    android:lineSpacingExtra="@dimen/linespace"
                    android:textColor="?attr/MainTextColor"
                    android:text="Hello there two line text for show here which I am typing for a test"
                    android:scrollHorizontally="true"
                    android:textSize="@dimen/textDetailQuote"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:background="?attr/dark_color"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="@dimen/likeIcon"
                        android:layout_height="@dimen/likeIcon"
                        android:layout_weight="1"
                        android:tint="?attr/List_Text"
                        android:src="@drawable/time_new_ic" />

                    <TextView
                        android:id="@+id/lblTimeCount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="00:00"
                        android:textStyle="bold"
                        android:textColor="?attr/List_Text"
                        android:textSize="@dimen/lblTime" />



                    <ImageView
                        android:layout_width="@dimen/likeIcon"
                        android:layout_height="@dimen/likeIcon"
                        android:layout_weight="1"
                        android:tint="?attr/List_Text"
                        android:src="@drawable/fav_new_ic" />

                    <TextView
                        android:id="@+id/lblLikeCount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="200"
                        android:textStyle="bold"
                        android:textColor="?attr/List_Text"
                        android:textSize="@dimen/lblTime" />

                    <ImageView
                        android:layout_width="@dimen/likeIcon"
                        android:layout_height="@dimen/likeIcon"
                        android:layout_weight="1"
                        android:tint="?attr/List_Text"
                        android:src="@drawable/share_new_ic" />

                    <TextView
                        android:id="@+id/lblShareCount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:textStyle="bold"
                        android:text="100"
                        android:textColor="?attr/List_Text"
                        android:textSize="@dimen/lblTime" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>


</RelativeLayout>

我尝试过在stackoverflow上列出的许多解决方案,但都没有起作用。如果我从列表项中删除 CardView ....Ripple Effect 会出现在 LinearLayout 的后面,但我仍想使用 CardView如果有人能帮我解决这个问题,请告诉我。谢谢。

注意:我已经使用这个解决方案解决了它。谢谢。


使您的 CardView 可点击并为其设置前景色。 - Sandip Fichadiya
3个回答

3

将涟漪设置为CardView上的前景可绘制对象:

android:foreground="?attr/selectableItemBackground"

这将允许水波纹在CardView内部的内容上方显示,这通常是您想要的CardView效果。


你是将它应用在CardView上还是RelativeLayout上?它只能在CardView上工作。 - Brian Yencho

2

创建波纹可绘制对象并将其设置为您布局的前景。

ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="@color/your_color"
    tools:targetApi="lollipop">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="@color/your_color" />
        </shape>
    </item>
</ripple>

将您的代码更改为以下内容:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.CardView
        android:clickable="true"
        android:foreground="@drawable/ripple"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        app:cardCornerRadius="6dp"
        app:cardElevation="4dp">

1
尝试将此代码设置为前台并测试。我尝试过,对我而言有效。 - Bhuvanesh BS
请查看我的更新回答。将ripple设置为cardview而不是相对布局@ Priya - Bhuvanesh BS

2
使用 android:background="?android:attr/selectableItemBackground" 来设置背景。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接