在RecyclerView中为棒棒糖之前的可绘制着色

Ksh*_*wal 1 android android-drawable android-recyclerview

我正在尝试在我的RecyclerView中使用以下代码使用可绘制着色

Drawable likeDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.ic_thumb_up);
Drawable likeWrappedDrawable = DrawableCompat.wrap(likeDrawable);
DrawableCompat.setTint(likeWrappedDrawable,ContextCompat.getColor(getActivity(), android.R.color.white));
holder.ivLike.setImageDrawable(likeWrappedDrawable);
Run Code Online (Sandbox Code Playgroud)

现在,所有这些工作都在onBindViewHolderRecyclerView Adapter的中完成。

我会根据该列表项的状态在三种颜色之间更改此色调。对于Lolipop及更高版本,这一切正常,但在此版本以下,列表项目的颜色是无法预测的。有时它显示正确的颜色,但刷新列表时有时会更改为其他颜色。

Anything im doing wrong here or the tinting thing on pre-lollipop is still unusable in this particular case?

Update Including the code from my onBindViewHolder

@Override
        public void onBindViewHolder(ViewHolder holder, int position) {
...

            Drawable likeDrawable =
                    ContextCompat.getDrawable(getActivity(), R.drawable.ic_thumb_up);


            Drawable likeWrappedDrawable = DrawableCompat.wrap(likeDrawable);


                holder.tvLikeCount.setTextColor(ResUtil.getColor(R.color.light_font,
                        getActivity()));

                DrawableCompat.setTint(likeWrappedDrawable,
                        ContextCompat.getColor(getActivity(), android.R.color.white));

                if (tweetModel.isFavorited()) {
                DrawableCompat.setTint(likeWrappedDrawable,
                        ContextCompat.getColor(getActivity(), android.R.color.holo_blue_light));
            }



            holder.ivLike.setImageDrawable(likeWrappedDrawable);

        }
Run Code Online (Sandbox Code Playgroud)

小智 6

在调用setTint()之前,在Drawable上调用mutate()

Drawable likeDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.ic_thumb_up).mutate();
Run Code Online (Sandbox Code Playgroud)

默认情况下,从同一资源加载的所有drawable实例均具有相同的状态;如果您修改一个实例的状态,则所有其他实例将收到相同的修改。 http://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()