【发布时间】: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