java - 如何在不更改 RecyclerView 中的主体的情况下更改 View Drawable 的背景颜色?

标签 java android android-recyclerview android-drawable

我有一个带有主体和颜色的标准可绘制对象。 但是每当我调用该方法时,我在 recyclerView 的绑定(bind)过程中更改此可绘制对象的颜色时遇到问题 holder.item.setBackgroundColor();setBackground 它甚至会根据需要更改颜色,但最终会完全改变我的初始 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>

实现形状的 View

<?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)持有人

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);

我需要什么:

enter image description here

我得到的:

enter image description here

最佳答案

在适配器中添加以下实用方法

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

并在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);
    
}

演示:

关于java - 如何在不更改 RecyclerView 中的主体的情况下更改 View Drawable 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65694277/

相关文章:

java - 使用 Java 的 Playframeowk 抛出 JPA 事务失败。由: there is no started appliaction引起

java - Java-无法调用编译错误方法

java - 线程 "main"java.lang.NoSuchFieldError : Factory question 中出现异常

android - 一旦应用程序运行并出现错误 ClassNotFoundException : Didn't find class application on path: DexPathList,应用程序立即崩溃

android - Recyclerview 在 Activity 或 fragment android 中使用按钮 Action 向上和向下按钮滚动

java - 如何获取对象或类的方法列表?

java - For循环变量问题

Android 风格及其工作原理

java - 垂直拖放到 RecyclerView 上后如何保留 firestore 中的项目位置

android - 如何在 Android RecyclerView 和 LayoutManager 中使用 scrollVerticallyBy()?