OnTouch Listener 移除 CardView 上的圆角
OnTouch Listener removes round corners on CardView
我将 onTouchListener
添加到我的 RecyclerView
适配器,它可以很好地改变颜色,但它也删除了 RecyclerView
的圆角。您可以在屏幕截图中看到它。
代码如下:
holder.cardViewRemaining.setOnTouchListener((v, event) -> {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(Color.parseColor("#f0f0f0"));
}
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(Color.WHITE);
}
return false;
});
How to programmatically round corners and set random background colors
您似乎不应该使用 setBackgroundColor
方法,而应该检索背景可绘制对象并设置其颜色。
您需要使用 setCardBackgroundColor()
.
holder.cardViewRemaining.setOnTouchListener((v, event) -> {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
holder.cardViewRemaining.setCardBackgroundColor(Color.parseColor("#f0f0f0"));
}
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
{
holder.cardViewRemaining.setCardBackgroundColor(Color.WHITE);
}
return false;
});
我将 onTouchListener
添加到我的 RecyclerView
适配器,它可以很好地改变颜色,但它也删除了 RecyclerView
的圆角。您可以在屏幕截图中看到它。
代码如下:
holder.cardViewRemaining.setOnTouchListener((v, event) -> {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(Color.parseColor("#f0f0f0"));
}
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(Color.WHITE);
}
return false;
});
How to programmatically round corners and set random background colors
您似乎不应该使用 setBackgroundColor
方法,而应该检索背景可绘制对象并设置其颜色。
您需要使用 setCardBackgroundColor()
.
holder.cardViewRemaining.setOnTouchListener((v, event) -> {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
holder.cardViewRemaining.setCardBackgroundColor(Color.parseColor("#f0f0f0"));
}
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
{
holder.cardViewRemaining.setCardBackgroundColor(Color.WHITE);
}
return false;
});