我试图获得一个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)
        }
    }

其他回答

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

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

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

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

只工作api 21和使用这个不使用这个列表行卡视图

android Cardview控件的Ripple事件:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp" />

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部分的答案。

如果你正在使用的minSdkVersion应用程序是9级,你可以使用:

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

相反,从第11级开始,你可以使用:

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

从文档:

clickable -定义该视图是否响应单击事件。必须为布尔值,“true”或“false”。 前景-定义在内容上绘制的可绘制对象。这可以用作覆盖。如果重力设置为填充,前景可绘制对象将参与内容的填充。