首页 > 解决方案 > 通过单击 RecyclerView 项对整个父视图产生波纹效果

问题描述

我有一个 CardView,其中包括 RecyclerView。当用户单击 RecyclerView 项目时,我尝试对整个 CardView(或整个 RecyclerView)产生涟漪效应,但涟漪效应仅适用于项目。

    <android.support.v7.widget.CardView
                android:id="@+id/cardview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:foreground="?attr/selectableItemBackground">

                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/list"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintBottom_toTopOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="parent"/>

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

标签: androidandroid-recyclerviewripple

解决方案


将以下属性添加到您的CardView.

<android.support.v7.widget.CardView
    ...
    android:clickable="true"
    android:focusable="true"
    android:background="?android:selectableItemBackground"/>

运行您的应用程序。它应该工作。


推荐阅读