Ripples not showing with selectableItemBackground as foreground on a CardView with a Android 5.0 device

走远了吗. 提交于 2019-12-05 02:52:34

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);

Now I get the ripple effect on 5.0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!