Colorful icon indicating copy to clipboard operation
Colorful copied to clipboard

个人觉得RecyclerView应该添加clear ViewHolder cache

Open Zhaoyy opened this issue 9 years ago • 13 comments

我之前也处理过这种类似主题变换的项目,如果不清楚cache,上拉或者下拉刷新的情况下会出现问题。

我的方法当时是: list.clear();//只是比你的clear多了一个 getRecycledViewPool().clear();;

Zhaoyy avatar Sep 10 '15 08:09 Zhaoyy

我在里面调用的是缓存视图的recycler的clear方法,晚点尝试一下。你这里的list是什么类型?

hehonghui avatar Sep 10 '15 23:09 hehonghui

我这里是整了一个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 avatar Sep 11 '15 01:09 Zhaoyy

@Zhaoyy 差不多 Colorful里面也调用了clear函数。不过没有getRecycledViewPool().clear();这个。

hehonghui avatar Sep 11 '15 04:09 hehonghui

我之前处理的时候发现不加这句会出现部分item theme不刷新的问题。

Zhaoyy avatar Sep 11 '15 07:09 Zhaoyy

@Zhaoyy 我也是调用了clear方法,如果没有加你那句确实是会出现部分item theme不刷新的问题,多谢分享

SherlockOy avatar Mar 01 '16 07:03 SherlockOy

我用的换肤框架,会有换肤后,调用 notifyDatasetChanged ,发现 有些ViewHoldre 是new的 ,有些viewHolder 是复用的,导致部分item 然后是旧的皮肤, 使用你的clear是不是可以解决

XiuWuZHuo avatar Mar 12 '16 03:03 XiuWuZHuo

@XiuWuZHuo you can have a try

Zhaoyy avatar Mar 18 '16 08:03 Zhaoyy

请教个问题,
我在做夜间模式时需要更改RecyclerView的item背景和字体的颜色,
我通过遍历item来更改结果出现了当前显示的item是改变了,
而滑上去发现,有的item还是原来的颜色,但当这些item滑出界面再滑回来时颜色也是会变的,

请问这是需要加上吗?

getRecycledViewPool().clear();

qq137712630 avatar Jun 05 '16 02:06 qq137712630

@qq137712630 内部 已经有了 getRecycledViewPool().clear(); 请确保你的Item中的各种颜色也是通过 自定义属性来配置的。

hehonghui avatar Jun 05 '16 03:06 hehonghui

@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();
    }

}

qq137712630 avatar Jun 05 '16 10:06 qq137712630

提问题的兄弟很 细心,我看demo都没注意这个bug

mux2 avatar Jul 08 '16 03:07 mux2

不同主题的view type 加上不同的offset,切换主题后自动创建新 view

s1ntoneli avatar Jan 13 '17 13:01 s1ntoneli

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);
        }

    }

看看这个方法,切换前调用一下

Blowing avatar Aug 22 '19 02:08 Blowing