Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Page重用问题 #84

Open
jimmysuncpt opened this issue Jul 16, 2019 · 0 comments
Open

Page重用问题 #84

jimmysuncpt opened this issue Jul 16, 2019 · 0 comments

Comments

@jimmysuncpt
Copy link

jimmysuncpt commented Jul 16, 2019

在使用Page无限循环时发现一个复用的问题:
问题描述:比如我在每个Item里放了一个Text控件和Image控件,现数据源有两个Item:第一个Item只设置了文字,第二个Item只设置了图片。当加载显示后,第一个和第二个都正常显示,但滑动到第三个时(也就是循环回来的第一个),图片和文字却同时显示了。
原因分析:跟了一下源代码发现,当滑动到第三个Item时,该Item使用的ViewHolder会复用前面的,而且复用的ViewHolder没有清空旧数据,这样导致了旧数据和新数据都显示在了一个Item的ViewHolder上,所以图片和文字都同时显示了。
解决方案:我目前的解决方案是找到PageViewadd(int pos, int index)方法,修改成如下代码:

    protected void add(int pos, int index) {
        int type = mAdapter.getType(pos);
        List<Adapter.ViewHolder> items = mItemCache.get(type);
        if (null != items && items.size() > 0) {
            items.remove(0);
        }
        Adapter.ViewHolder vh = mAdapter.onCreateViewHolder(type);
        vh.mType = type;
        vh.mPos = pos;
        mAdapter.onBindViewHolder(vh, pos);
        if (index < 0) {
            this.addView(vh.mItemView);
        } else {
            this.addView(vh.mItemView, index);
        }
    }

但这样修改会有一个问题:我每次都重新创建ViewHolder,并不会复用了,这样有一定的性能问题,希望官方合理地解决这个bug。

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

No branches or pull requests

1 participant