我有一个recyclerview适配器,可以根据条件更改整个行的背景颜色。该行是可单击的,并设置为android:background="?attr/selectableItemBackground"
。现在,如果我更改颜色(例如绿色),则会失去波纹效果。如何以编程方式设置它?我的情况下需要带有绿色bg的白色波纹。
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));
}
已解决将其设置为背景可绘制对象:
<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>