我正在尝试在我的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);
现在所有这些都在RecyclerView适配器的onBindViewHolder
方法中完成。
我根据列表项的状态将此着色更改为三种颜色。这在Lolipop及以上版本中运行良好,但在此版本以下,列表项的颜色是不可预测的。有时它会显示正确的颜色,但在刷新列表时,有时会变成其他颜色。
我做错了什么或者在早期版本的着色处理在这种情况下仍无法使用?
更新
包含我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);
}
onBindViewHolder
的完整代码吗? - Inder Kumar Rathore