【问题标题】:Change Ripple Color programmatically according to background根据背景以编程方式更改波纹颜色
【发布时间】:2020-02-22 17:04:43
【问题描述】:

我有一个 recyclerview 适配器,可以根据条件更改整个行的背景颜色。该行是可点击的,并设置了android:background="?attr/selectableItemBackground"。现在,如果我改变颜色(例如绿色),我会失去涟漪效应。如何以编程方式设置它?在我的情况下,我需要一个带有绿色背景的白色波纹。

if (dish.isSelected()) {
    // GREEN BACKGROUND NOT WORKING (NO RIPPLE AT ALL)
    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);
    viewHolderDish.itemView.setBackgroundResource(typedValue.resourceId);
    viewHolderDish.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
    viewHolderDish.itemView.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.green)));
} else {
    // TRANSPARENT BACKGROUND WORKS
    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);
    viewHolderDish.itemView.setBackgroundResource(typedValue.resourceId);
    viewHolderDish.itemView.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
}

【问题讨论】:

    标签: android android-layout android-styles android-viewholder android-adapterview


    【解决方案1】:

    解决了将其设置为背景可绘制的问题:

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@android:color/white">
        <item android:id="@android:id/background">
            <shape android:shape="rectangle">
                <solid android:color="@color/green" />
            </shape>
        </item>
        <item
            android:id="@android:id/mask"
            android:drawable="@android:color/white" />
    </ripple>
    

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多