在RecyclerView中更改单个可绘制对象的颜色将更改所有可绘制对象 [英] Changing color of single drawable in RecyclerView will change all drawables

查看:63
本文介绍了在RecyclerView中更改单个可绘制对象的颜色将更改所有可绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just tried to change the color of my drawable inside my row depending on a value but instead of one drawable the adapter changed all of them.

这是我的适配器:

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

    }

}

知道任何人该怎么办?谢谢您的帮助!

Knows anybody what to do? Thank you for your help!!!

推荐答案

这是一种缓存.从 Android文档:

It's sort of caching. From the Android docs:

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

if you instantiate two Drawable objects from the same image resource, then change a property (such as the alpha) for one of the Drawables, then it will also affect the other. So when dealing with multiple instances of an image resource, instead of directly transforming the Drawable, you should perform a tween animation.

Drawable.mutate()之后创建应该可以解决该问题.

Drawable.mutate() after creating should fix the issue.

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

A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

类似这样的东西:

Drawable box = ContextCompat.getDrawable(context, R.drawable.markbox).mutate();
Drawable boxBorder = ContextCompat.getDrawable(context, R.drawable.markbox_border).mutate();

这篇关于在RecyclerView中更改单个可绘制对象的颜色将更改所有可绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆