Colorful
Colorful copied to clipboard
个人觉得RecyclerView应该添加clear ViewHolder cache
我之前也处理过这种类似主题变换的项目,如果不清楚cache,上拉或者下拉刷新的情况下会出现问题。
我的方法当时是: list.clear();//只是比你的clear多了一个 getRecycledViewPool().clear();;
我在里面调用的是缓存视图的recycler的clear方法,晚点尝试一下。你这里的list是什么类型?
我这里是整了一个recyclerView的子类。 public void clear() { try { Field recycler = RecyclerView.class.getDeclaredField("mRecycler"); recycler.setAccessible(true); Method localMethod = Class.forName("android.support.v7.widget.RecyclerView$Recycler") .getDeclaredMethod("clear"); localMethod.setAccessible(true); localMethod.invoke(recycler.get(this)); } catch (NoSuchFieldException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); }
getRecycledViewPool().clear();
}
@Zhaoyy 差不多 Colorful里面也调用了clear函数。不过没有getRecycledViewPool().clear();这个。
我之前处理的时候发现不加这句会出现部分item theme不刷新的问题。
@Zhaoyy 我也是调用了clear方法,如果没有加你那句确实是会出现部分item theme不刷新的问题,多谢分享
我用的换肤框架,会有换肤后,调用 notifyDatasetChanged ,发现 有些ViewHoldre 是new的 ,有些viewHolder 是复用的,导致部分item 然后是旧的皮肤, 使用你的clear是不是可以解决
@XiuWuZHuo you can have a try
请教个问题,
我在做夜间模式时需要更改RecyclerView的item背景和字体的颜色,
我通过遍历item来更改结果出现了当前显示的item是改变了,
而滑上去发现,有的item还是原来的颜色,但当这些item滑出界面再滑回来时颜色也是会变的,
请问这是需要加上吗?
getRecycledViewPool().clear();
@qq137712630 内部 已经有了 getRecycledViewPool().clear(); 请确保你的Item中的各种颜色也是通过 自定义属性来配置的。
@hehonghui
最后我在 `clearRecyclerViewRecyclerBin下, 增加
((RecyclerView) rootView).getRecycledViewPool().clear();``解决了
public class RecyclerViewSetter extends ViewGroupSetter {
public RecyclerViewSetter(ViewGroup targetView, int resId) {
super(targetView, resId);
}
public RecyclerViewSetter(ViewGroup targetView) {
super(targetView);
}
@Override
protected void clearRecyclerViewRecyclerBin(View rootView) {
super.clearRecyclerViewRecyclerBin(rootView);
((RecyclerView) rootView).getRecycledViewPool().clear();
}
}
提问题的兄弟很 细心,我看demo都没注意这个bug
不同主题的view type 加上不同的offset,切换主题后自动创建新 view
public void setMaxRecycledViews(int viewType, int max) { RecyclerView.RecycledViewPool.ScrapData scrapData = this.getScrapDataForType(viewType); scrapData.mMaxScrap = max; ArrayList scrapHeap = scrapData.mScrapHeap;
while(scrapHeap.size() > max) {
scrapHeap.remove(scrapHeap.size() - 1);
}
}
看看这个方法,切换前调用一下