android – 在RecyclerView中更改单个drawable的颜色将更改所有drawable

我只是试图根据一个值改变我的行内部drawable的颜色,但是不是一个drawable,适配器改变了所有这些颜色.

这是我的适配器:

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

    }

}

知道怎么做?谢谢您的帮助!!!

解决方法

这是一种缓存.从 Android docs

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()创建后应解决问题.

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...