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

当XRecyclerView 添加超过两个headers 将触发 IllegalStateException: ViewHolder views must not be attached when created. #404

Open
Krosxx opened this issue Mar 30, 2020 · 0 comments

Comments

@Krosxx
Copy link

Krosxx commented Mar 30, 2020

复现场景:

先查看一个header的XRecyclerView(AActivity);再查看两个header的XRecyclerView(BActivity)就会触发 IllegalStateException: ViewHolder views must not be attached when created.

经过分析发现 XRecyclerView.sHeaderTypes 为 被 static 修饰。

public void addHeaderView(View view) {
    if(mHeaderViews == null || sHeaderTypes == null)
        return;
    sHeaderTypes.add(HEADER_INIT_INDEX + mHeaderViews.size());
    mHeaderViews.add(view);
    if (mWrapAdapter != null) {
        mWrapAdapter.notifyDataSetChanged();
    }
}

addHeaderView 中 sHeaderTypes.add 和 mHeaderViews.add 肯定会由于多个列表的多次addHeaderView导致数据不同步。最后出现

第一次加载列表 sHeaderTypes 已为 [10002]
第二次加载列表 sHeaderTypes 为 [10002,10002,10003]

且经过 XRecyclerView.WrapAdaptergetItemViewType -> onCreateViewHolder -> getHeaderViewByType
会导致两次获取到同一个 headerView 。

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

1 participant