Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Zhaoyy opened this issue Sep 10, 2015 · 13 comments
Open

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

Zhaoyy opened this issue Sep 10, 2015 · 13 comments

Comments

@Zhaoyy
Copy link

Zhaoyy commented Sep 10, 2015

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

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

@hehonghui
Copy link
Owner

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

@Zhaoyy
Copy link
Author

Zhaoyy commented Sep 11, 2015

我这里是整了一个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();

}

@hehonghui
Copy link
Owner

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

@Zhaoyy
Copy link
Author

Zhaoyy commented Sep 11, 2015

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

@SherlockOy
Copy link

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

@XiuWuZHuo
Copy link

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

@Zhaoyy
Copy link
Author

Zhaoyy commented Mar 18, 2016

@XiuWuZHuo you can have a try

@qq137712630
Copy link

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

请问这是需要加上吗?

getRecycledViewPool().clear();

@hehonghui
Copy link
Owner

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

@qq137712630
Copy link

qq137712630 commented Jun 5, 2016

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

}

@mux2
Copy link

mux2 commented Jul 8, 2016

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

@s1ntoneli
Copy link

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

@Blowing
Copy link

Blowing commented Aug 22, 2019

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

    }

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants