Changing color of single drawable in RecyclerView will change all drawables

后端 未结 2 1518
执笔经年
执笔经年 2021-02-13 23:26

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.

Here is my Adapter:

2条回答
  •  鱼传尺愫
    2021-02-13 23:34

    Thanks to Sergey that guided me to the solution. I share what I did in onBindViewHolder method.

    final Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_icon).mutate();
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        holder.image.setBackground(drawable);
    } else {
        holder.image.setBackgroundDrawable(drawable);
    }
    

提交回复
热议问题