微澜不selectableItemBackground显示为前景上CardView了Android 5.0设备 [英] Ripples not showing with selectableItemBackground as foreground on a CardView with a Android 5.0 device

查看:212
本文介绍了微澜不selectableItemBackground显示为前景上CardView了Android 5.0设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一台Nexus 5.运行此这里的code我CardView部分:

I'm running this on a Nexus 5. Here's part of the code for my CardView:

        CardView cardView = new CardView(getActivity());
        cardView.setRadius(4);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 400);
        lp.setMargins(32, 16, 32, 16);
        cardView.setLayoutParams(lp);
        cardView.setContentPadding(50, 50, 50, 50);
        ...
        cardView.setForeground(selectedItemDrawable);

和这里就是我得到selectedItemDrawable:

And here's how I get the selectedItemDrawable:

        int[] attrs = new int[] { R.attr.selectableItemBackground };
        TypedArray ta = getActivity().obtainStyledAttributes(attrs);
        selectedItemDrawable = ta.getDrawable(0);
        ta.recycle();

当我一敲卡,这是应该来与selectedItemDrawable不会出现波纹(它看起来完全一样,没有前台集)。我运行5.0,所以这似乎很奇怪,因为appcompat文档只能说,它不与pre-棒棒糖设备的正常工作。有谁知道为什么是这样?最低API级别为16,目标定位21。

When I tap the card, the ripple that's supposed to come with the selectedItemDrawable does not appear (it looks exactly the same as without the foreground set). I am running 5.0, so this seems strange, as the appcompat docs only say that it doesn't work with pre-Lollipop devices. Does anybody know why this is the case? Minimum API level is 16, targetting 21.

推荐答案

原来,我是分享与多个cardviews的绘制对象的我的实例。这是通过使用一个getSelectedItemDrawable方法返回一个新实例解决的问题:

It turned out that I was sharing my instance of the Drawable with multiple cardviews. This was resolved by returning a new instance using a getSelectedItemDrawable method:

    public Drawable getSelectedItemDrawable() {
        int[] attrs = new int[]{R.attr.selectableItemBackground};
        TypedArray ta = getActivity().obtainStyledAttributes(attrs);
        Drawable selectedItemDrawable = ta.getDrawable(0);
        ta.recycle();
        return selectedItemDrawable;
    }

然后将其设置为前台编程方式:

Then setting it as the foreground programatically:

        cardView.setForeground(getSelectedItemDrawable());
        cardView.setClickable(true);

现在我得到的涟漪效应在5.0。

Now I get the ripple effect on 5.0.

这篇关于微澜不selectableItemBackground显示为前景上CardView了Android 5.0设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆