Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 可拉深染色的RecyclerView,适用于棒棒糖制作前_Android_Android Drawable_Android Recyclerview - Fatal编程技术网

Android 可拉深染色的RecyclerView,适用于棒棒糖制作前

Android 可拉深染色的RecyclerView,适用于棒棒糖制作前,android,android-drawable,android-recyclerview,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(getAct

我正在尝试使用我的
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及以上版本,但低于此版本,列表项的颜色是不可预测的。有时它显示正确的颜色,但在刷新列表时,有时它会更改为其他颜色

在这种情况下,我做错了什么,或者棒棒糖前的着色功能仍然无法使用

更新 包括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);

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

默认情况下,从同一资源加载的所有drawables实例共享一个公共状态;如果修改一个实例的状态,所有其他实例将收到相同的修改。
TL;博士如果您担心效率,请使用一个您可以从中获取的单一类

需要做的不仅仅是
.mutate()
正如@Vladimir所述,您必须调用
drawable.mutate()
以获得一个隔离状态,否则对您的drawable属性所做的任何更改都将反映在共享相同状态的所有其他drawable上

这种行为实际上是有充分理由的,其中之一就是内存效率,在某些情况下,您希望保持这种效率

例如,您可能会有一个
RecyclerView
,其中的项目使用相同的绘图工具,但根据某些属性(例如成功、失败、警告、信息等),每个项目的颜色不同。如果你在这种情况下,改变你的每一个项目的绘图不是最好的解决方案。您可以考虑创建所有可能的绘图,所有可能的色调,但您可能会创建无用的ONCE。如果你是那样的话,我会掩护你的

进入
TintedictionCache
更好的解决方案是使用某种类型的可绘制缓存,其中每个唯一着色的可绘制缓存仅在需要时创建,然后高效地缓存以满足后续需要,直到您不再需要它或系统想要回占用的内存

我已经在年实施了这个。使用起来很简单:

// Get an instance
TintedIconCache cache = TintedIconCache.getInstance();

// Will be fetched from the resources
Drawable backIcon = cache.fetchTintedIcon(context, R.drawable.icon, R.color.black));

// Will be fetched from the resources as well
Drawable bleuIcon = cache.fetchTintedIcon(context, R.drawable.icon, R.color.bleu));
   
// Will be fetched from the cache!!!
Drawable backIconTwo = cache.fetchTintedIcon(context, R.drawable.icon, R.color.back));


考虑查看引擎盖下的工作原理。

ic_thumb_向上是什么类型的可牵引式?仅仅是一个PNG,或者一个基于XML的可绘制文件?我们能得到
onBindViewHolder
的完整代码吗?今天将发布代码,并用所需代码更新我的问题。遗憾的是,我无法分享更多信息。你可以从链接中尝试我的答案:我上次尝试过使用它,但没有多大帮助。我会再给它一次机会。
TintedIconCache cache = TintedIconCache.getInstance();
Drawable myTintedDrawable = cache.fetchTintedIcon(context, R.drawable.icon_01, R.color.color_01));
// Get an instance
TintedIconCache cache = TintedIconCache.getInstance();

// Will be fetched from the resources
Drawable backIcon = cache.fetchTintedIcon(context, R.drawable.icon, R.color.black));

// Will be fetched from the resources as well
Drawable bleuIcon = cache.fetchTintedIcon(context, R.drawable.icon, R.color.bleu));
   
// Will be fetched from the cache!!!
Drawable backIconTwo = cache.fetchTintedIcon(context, R.drawable.icon, R.color.back));