简体   繁体   English

如何在不改变 recyclerView 主体的情况下更改 View Drawable 的背景颜色?

[英]How to change the background color of a View Drawable without changing its body in recyclerView?

I have a standard drawable with a body and a color.我有一个带有主体和颜色的标准可绘制对象。 but I'm having trouble changing the color of this drawable in the Bind process of my recyclerView , whenever I invoke the method holder.item.setBackgroundColor ();但是每当我调用方法recyclerView holder.item.setBackgroundColor (); or setBackground it even changes the color as needed, but it ends up totally changing the shape of my initial drawble.setBackground它甚至会根据需要更改颜色,但最终会完全改变我最初的 drawble 的形状。

My shape Drawble:我的形状 Drawble:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/holo_orange_dark" />
    <size
        android:width="120dp"
        android:height="120dp"
        />
</shape>

the view that implements the shape实现形状的视图

<?xml version="1.0" encoding="utf-8"?>

<View
    android:id="@+id/item_cores"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="@drawable/cores_drawable"
    android:layout_margin="10dp"
    android:backgroundTint="@color/black"
    xmlns:android="http://schemas.android.com/apk/res/android" />

class holder class 支架

class coresHolder extends RecyclerView.ViewHolder {
        cores colors;
        View item;

        public coresHolder(View itemView) {
            super(itemView);
            itemClick(itemView);
            item= itemView.findViewById(R.id.item_cores);

        }

        private void itemClick(View itemView) {
            itemView.setOnClickListener(v -> {
                onClickListernet.onItemClick(colors);
            });
        }

        public void vincule(cores colors) {
            this.colors =colors;
            
        }


    }
// bind
    @Override
    public void onBindViewHolder(coresHolder holder, int position) {
        cores cores=list.get(position);
        holder.vincule(cores);
      //ps only a test change color to holder
        holder.item.setBackgroundColor(R.color.purple_700);

What I need:我需要的:

在此处输入图像描述

What I got:我得到了什么:

在此处输入图像描述

Add the below utility method in the adapter在适配器中添加以下实用程序方法

public static void setDrawableColor(Context context, Drawable drawable, int color) {
    Drawable drawableWrap = DrawableCompat.wrap(drawable).mutate();
    DrawableCompat.setTint(drawableWrap, ContextCompat.getColor(context, color));
}

And use it as follows in onBindViewHolder并在onBindViewHolder中按如下方式使用

@Override
public void onBindViewHolder(coresHolder holder, int position) {
    cores cores=list.get(position);
    holder.vincule(cores);
   
    // Changing background drawable
    setDrawableColor(holder.item.getContext(), holder.item.getBackground(), R.color.purple_700);
    
}

Demo:演示:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将可绘制集的背景/颜色更改为视图的背景? - How to change background/color of the drawable set as background of a view? 如何在RemoteViews中更改ImageView的背景颜色而不更改其形状? - How to change the background's color of an ImageView in RemoteViews without changing its shape? 在RecyclerView的OnBindViewHolder中更改视图的文本颜色或背景颜色 - Changing text color or background color of view in OnBindViewHolder of RecyclerView 更改JButton的颜色而不更改其形状 - Change color of JButton without changing its shape 如何在 Drawable 中改变颜色? - How to change color in Drawable? 如何以编程方式更改 RadioButton 背景的可绘制颜色? - How can I change the drawable color of RadioButton's background programmatically? 以编程方式用作背景时如何更改 ImageButton 可绘制颜色? - How to change ImageButton drawable color when used as background programmatically? 如何在不更改Android中操作栏颜色的情况下更改应用程序背景颜色 - How can I change the app background color without changing the action bar color in android 如何在RecyclerView元素中更改标题背景颜色onClick? - How to change header background color onClick in a RecyclerView element? 如何在不更改javafx边框的情况下更改TextField的背景颜色? - How do you change the background color of a TextField without changing the border in javafx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM