我试图获得一个CardView显示涟漪效应时,通过设置android:背景属性在活动XML文件中描述这里在android开发人员页面,但它不起作用。没有动画,但是onClick中的方法被调用。我还尝试按照这里的建议创建一个ripple.xml文件,但结果相同。

出现在活动的XML文件中的CardView:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="155dp"
    android:layout_height="230dp"
    android:elevation="4dp"
    android:translationZ="5dp"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:onClick="showNotices"
    android:background="?android:attr/selectableItemBackground"
    android:id="@+id/notices_card"
    card_view:cardCornerRadius="2dp">

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

我对android开发相对陌生,所以我可能犯了一些明显的错误。


当前回答

在xml中添加以下内容:

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

并添加到适配器(如果是您的情况)

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val attrs = intArrayOf(R.attr.selectableItemBackground)
            val typedArray = holder.itemView.context.obtainStyledAttributes(attrs)
            val selectableItemBackground = typedArray.getResourceId(0, 0)
            typedArray.recycle()

            holder.itemView.isClickable = true
            holder.itemView.isFocusable = true
            holder.itemView.foreground = holder.itemView.context.getDrawable(selectableItemBackground)
        }
    }

其他回答

使用Material Cardview代替,它扩展了Cardview并提供了多个新功能,包括默认的可点击效果:

<com.google.android.material.card.MaterialCardView>

...

</com.google.android.material.card.MaterialCardView>

依赖关系(它可以使用到API 14来支持旧设备):

implementation 'com.google.android.material:material:1.0.0'

涟漪效应在您正在使用的appcompat支持库中被省略了。如果你想看到波纹,请使用Android L版本并在Android L设备上进行测试。根据AppCompat v7网站:

“为什么前棒棒糖时代没有涟漪?” 很多让RippleDrawable顺利运行的是Android 5.0的新RenderThread。为了优化之前Android版本的性能,我们暂时放弃了RippleDrawable。”

点击这个链接了解更多信息

如果你想在(child) Card视图中添加任何元素,你可以使用:

<ImageView
android:background="?android:attr/selectableItemBackground"

但如果你需要整个卡视图,这样做:

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

如果你用两种颜色,颜色会比其他颜色深。

然而,最简单的方法是

com.google.android.material.card.MaterialCardView

你应该在CardView中添加以下内容:

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

我设法通过以下方法在cardview上获得涟漪效应:

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:clickable="true" 
    android:foreground="@drawable/custom_bg"/>

对于您在上面代码中看到的custom_bg,必须为lollipop(在可绘制的v21包中)和prelollipop(在可绘制的包中)设备定义一个XML文件。 对于drawable-v21包中的custom_bg,代码如下:

<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
<item
    android:id="@android:id/mask"
    android:drawable="@android:color/white"/>
</ripple>

对于可绘制包中的custom_bg,代码为:

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

<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/colorHighlight"></solid>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/navigation_drawer_background"></solid>
    </shape>
</item>
</selector>

在前棒棒糖设备上,你会有一个固定的点击效果在棒棒糖设备上,你会在卡片视图上有一个涟漪效应。