我试图获得一个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开发相对陌生,所以我可能犯了一些明显的错误。


当前回答

我设法通过以下方法在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>

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

其他回答

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

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

添加这两个类似的代码工作就像一个魅力的任何视图,如按钮,线性布局,或CardView只要把这两行,看看魔法…

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

For those searching for a solution to the issue of the ripple effect not working on a programmatically created CardView (or in my case custom view which extends CardView) being shown in a RecyclerView, the following worked for me. Basically declaring the XML attributes mentioned in the other answers declaratively in the XML layout file doesn't seem to work for a programmatically created CardView, or one created from a custom layout (even if root view is CardView or merge element is used), so they have to be set programmatically like so:

private class MadeUpCardViewHolder extends RecyclerView.ViewHolder {
    private MadeUpCardView cardView;

    public MadeUpCardViewHolder(View v){
        super(v);

        this.cardView = (MadeUpCardView)v;

        // Declaring in XML Layout doesn't seem to work in RecyclerViews
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int[] attrs = new int[]{R.attr.selectableItemBackground};
            TypedArray typedArray = context.obtainStyledAttributes(attrs);
            int selectableItemBackground = typedArray.getResourceId(0, 0);
            typedArray.recycle();

            this.cardView.setForeground(context.getDrawable(selectableItemBackground));
            this.cardView.setClickable(true);
        }
    }
}

MadeupCardView将CardView Kudos扩展到TypedArray部分的答案。

使用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'

对我来说,将前景添加到CardView没有工作(原因未知:/)

将相同的元素添加到它的子布局中就可以了。

代码:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:focusable="true"
    android:clickable="true"
    card_view:cardCornerRadius="@dimen/card_corner_radius"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:id="@+id/card_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:padding="@dimen/card_padding">

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