更改 RecyclerView 中单个可绘制对象的颜色将更改所有可绘制对象

2024-01-30

我只是尝试根据一个值更改行内可绘制对象的颜色,但适配器更改了所有可绘制对象,而不是一个可绘制对象。

这是我的适配器:

public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ReportViewHolder> {

    DataBaseHelper dataBase;

    private LayoutInflater inflater;
    List<ChoosedSubject> data = Collections.emptyList();
    Context context;
    OnItemClickListener itemClickListener;

    public ReportAdapter(Context context, List<ChoosedSubject> data, OnItemClickListener itemClickListener) {
        inflater = LayoutInflater.from(context);
        this.data = data;
        this.context = context;
        this.itemClickListener = itemClickListener;
    }

    @Override
    public ReportViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.report_cell, parent, false);
        ReportViewHolder holder = new ReportViewHolder(view);

        dataBase = new DataBaseHelper(context);

        return holder;
    }

    //Set Data inside RecyclerView
    @Override
    public void onBindViewHolder(ReportViewHolder holder, int position) {
        ChoosedSubject current = data.get(position);
        Grades grades = new Grades(context);

        Resources resources = context.getResources();
        int iconColor;
        Drawable icon;

        icon = ContextCompat.getDrawable(context, dataBase.getSpecificChoosedSubjectAppendingToName(current.getName()).get(0).getChoosedIcon());

        if (dataBase.getSpecificChoosedSubjectAppendingToName(current.getName()).get(0).getChoosedIcon() != R.drawable.subject_default) {
            iconColor = resources.getColor(dataBase.getSpecificChoosedSubjectAppendingToName(current.getName()).get(0).getChoosedColor());
            icon.setColorFilter(iconColor, PorterDuff.Mode.SRC_IN);
            holder.icon.setBackground(icon);
        } else {
            holder.icon.setImageResource(R.drawable.subject_default);
        }

        holder.subject.setText(current.getName().toString());

        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumFractionDigits(0);
        formatter.setMaximumFractionDigits(0);
        String output = formatter.format(dataBase.getSpecificChoosedSubjectAppendingToName(current.getName()).get(0).getAverage());

        int formattedValue = Integer.valueOf(output);


        //CHANGING COLOR DEPENDING ON VALUE
        int boxColor = 0;
        Drawable box = ContextCompat.getDrawable(context, R.drawable.markbox);
        Drawable boxBorder = ContextCompat.getDrawable(context, R.drawable.markbox_border);

        if (formattedValue >= 10) {
            boxColor = resources.getColor(R.color.positive);
        } else if (formattedValue >= 4 && formattedValue <= 9) {
            boxColor = resources.getColor(R.color.neutral);
        } else if (formattedValue < 4) {
            boxColor = resources.getColor(R.color.negative);

        }

        box.setAlpha(204);
        box.setColorFilter(boxColor, PorterDuff.Mode.SRC_IN);
        boxBorder.setColorFilter(boxColor, PorterDuff.Mode.SRC_IN);

        holder.markbox.setImageDrawable(box);
        holder.markboxBorder.setImageDrawable(boxBorder);


        holder.average.setText(output);
        holder.average.setTypeface(EasyFonts.robotoBlack(context));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public class ReportViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView subject;
        ImageView icon;
        ImageView markbox;
        ImageView markboxBorder;
        TextView average;

        public ReportViewHolder(View itemView) {
            super(itemView);
            subject = (TextView) itemView.findViewById(R.id.report_subject);
            icon = (ImageView) itemView.findViewById(R.id.report_icon);
            markbox = (ImageView) itemView.findViewById(R.id.report_markbox);
            markboxBorder = (ImageView) itemView.findViewById(R.id.report_markbox_border);
            average = (TextView) itemView.findViewById(R.id.report_average);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            itemClickListener.onItemClick(v, this.getAdapterPosition());
        }

    }

}

有人知道该怎么办吗?感谢您的帮助!!!


这是一种缓存。来自安卓文档 http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables-from-images:

如果您从同一图像资源实例化两个 Drawable 对象,然后更改其中一个 Drawable 的属性(例如 alpha),那么它也会影响另一个。因此,在处理图像资源的多个实例时,您应该执行补间动画,而不是直接转换Drawable。

Drawable.mutate() https://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()创建后应该可以解决问题。

保证可变可绘制对象不会与任何其他可绘制对象共享其状态。当您需要修改从资源加载的可绘制对象的属性时,这特别有用。默认情况下,从同一资源加载的所有可绘制对象实例共享一个公共状态;如果修改一个实例的状态,所有其他实例都会收到相同的修改。

像这样的事情:

Drawable box = ContextCompat.getDrawable(context, R.drawable.markbox).mutate();
Drawable boxBorder = ContextCompat.getDrawable(context, R.drawable.markbox_border).mutate();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改 RecyclerView 中单个可绘制对象的颜色将更改所有可绘制对象 的相关文章

随机推荐

  • 我可以使用什么工具来分析内存使用情况? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个使用 Visual Studio 2008 使用 C 编写的 Windows 应用程序 我想获取内存使用情况的统计信息 以查找内存
  • 重命名(别名)数组元素 C

    不确定什么是 良好实践 或被认为更 正确 我有一个数组 我想通过 arrayname 以外的名称访问各个元素 我可以使用 defines 或指针 也可能使用其他方式 Example define value1 myarray 1 int m
  • 在 html 电子邮件中发送个性化图像的推荐方式是什么?

    我知道已经有人问过类似的问题 但答案几乎总是相同的 您需要在服务器上共享图像并从电子邮件中链接到它 为了我的目的 我不能那样做 图像需要针对我发送电子邮件的每个用户进行个性化 因此将为每个用户动态生成电子邮件 并且不会始终相同 我无法共享图
  • LINQ to SQL 通配符

    如何在 LINQ To SQL lambda 表达式中构建通配符 这就是我目前所拥有的 var query from log in context Logs select log foreach string filter in Custo
  • 将列表项转换为元组

    我有一个这样的清单 February 01 2011 February 28 2011 March 01 2011 March 31 2011 我想将其转换为 February 01 2011 February 28 2011 March
  • 如何从数组中获取唯一值

    请注意这是针对 OSX 的 不适用于 iOS 我在其他问题中查看并尝试了一些解决方案 但似乎没有一个对我有用 因此 我想从数组中获取一组独特的年份 我的代码是这样的 NSMutableArray unique NSMutableArray
  • 使用 PEAR/Mail_Queue 发送 10,000 多封电子邮件的最佳方式

    我有一个 cron 它生成整个邮件信息并使用以下命令放入数据库表中 mail queue gt put 可以选择在发送电子邮件后将其删除 这是我需要一点帮助的地方 在获得上述信息后 发送电子邮件的最佳方式是什么 运行 mail queue
  • 自动布局:layoutMarginsGuide

    如何重写视觉格式 addConstraints NSLayoutConstraint constraintsWithVisualFormat label options AlignAllBaseline metrics nil views
  • 为什么我的列表项项目符号与浮动元素重叠

    我有一个 XHTML Strict 页面 其中我将图像与常规文本段落一起浮动 一切都很顺利 除非使用列表而不是段落 列表的项目符号与浮动图像重叠 更改列表或列表项的边距没有帮助 边距是从页面左侧开始计算的 但浮动会将列表项推到右侧insid
  • 无法使用 Apache PDFBOX 验证数字签名

    我是使用数字签名的新手 在其中一个项目中 我们使用 Apache PdfBox 来处理数字签名的 pdf 文件 虽然我们可以测试所有功能 但签名 pdf 文件的验证是我们无法破解的 我们使用 BouncyCastle 作为提供者 下面是代码
  • Tomcat连接器connectionTimeout和keepAliveTimeout之间的关系

    我想知道tomcat连接器的connectionTimeout和keepAliveTimeout属性之间的关系是什么 它们是否应该设置为相同的值 因为它是默认值 keepAliveTimeout 小于 connectionTimeout 有
  • 我的 PHP 会话不会保存在 hostgator 上

    意料之外 如何解决这个问题 我的会话无法在 Hostgator 服务器上运行 但在其他服务器上它工作正常 这是代码示例
  • 通过 cython 从 c 调用 python 代码

    所以我想通过 cython 从 c 调用一些 python 代码 我已经设法从 c 调用 cython 代码 我还可以从 cython 调用 python 代码 但当我把它们加在一起时 有些东西就不见了 这是我的Python代码 quack
  • 解析 Pandas DataFrame 中的日期/时间字符串

    我有以下 Pandas 系列日期 时间 pd DataFrame GMT 13 Feb 20089 30 AM 22 Apr 20098 30 AM 14 Jul 20108 30 AM 01 Jan 20118 30 AM GMT 13
  • 在 Heroku Laravel 实例上设置 HTTPS 重定向

    我有一个在 apache Heroku 实例上运行的 Laravel 5 版本 我试图确保所有流量都通过 https 转换 但是我非常迷失 我已成功启动并运行 SSL 证书 但是 使用 Heroku 您无法直接编辑服务器上的 htacces
  • 计算零膨胀的 glmmTMB 预测的置信区间

    我正在尝试计算零通胀的 glmmTMB 模型预测的置信区间 我浏览了 github 上发布的一些问题以及描述 glmmTMB 的原始论文 然而 glmmTMB 似乎发生了轻微的变化 我对正确的使用方法感到非常困惑 这是我到目前为止所看到的
  • 使用 docker-compose 删除指定卷?

    如果我有一个 docker compose 文件 例如 version 3 services postgres image postgres 9 4 volumes db data var lib db volumes db data 然后
  • DIV 绝对定位 - 浏览器窗口调整大小后保持位置

    我正在以绝对位置显示 div my label list style none list style type none position absolute top 2px left 10px width 20px height 20px
  • 如何在 Angular Material 中使用

    我想知道如何使用材质的图标 因为这不起作用
  • 更改 RecyclerView 中单个可绘制对象的颜色将更改所有可绘制对象

    我只是尝试根据一个值更改行内可绘制对象的颜色 但适配器更改了所有可绘制对象 而不是一个可绘制对象 这是我的适配器 public class ReportAdapter extends RecyclerView Adapter