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

AutoDisposePagedListAdapter 生命周期问题 #6

Open
windows7lake opened this issue Aug 9, 2019 · 0 comments
Open

AutoDisposePagedListAdapter 生命周期问题 #6

windows7lake opened this issue Aug 9, 2019 · 0 comments

Comments

@windows7lake
Copy link

在使用AutoDisposePagedListAdapter对RecyclerView.ViewHolder进行管理时,发现一个问题:
在CustomeAdapter的CustomeViewHolder中使用如下代码设置item的点击事件时,此时如果更新pageList中的数据(是更新不是插入或者删除),RecyclerView会走onBindViewHolder(),之后会导致item的点击事件失效。

 view.clicks()
    .map { position }
    .autoDisposable(this)
    .subscribe(subject)

进一步分析之后,大致原因在于更新数据时,RecyclerView会先onBindViewHolder(),再调用onViewRecycled()对item资源进行回收。OnUnbindsPosition使被更新的Item点击事件失效了。虽然大致知道了问题,如果只是更新一项的话,我通过记录position可以解决问题,但是当更新的项有多个时就无从下手了。
AutoDisposePagedListAdapter中的代码:

    @CallSuper
    override fun onBindViewHolder(holder: VH, position: Int) {
        when (holder) {
            is AutoDisposeViewHolder -> {
                holder.resetBindEvents(this)
            }
        }
    }

    @CallSuper
    override fun onViewRecycled(holder: VH) {
        super.onViewRecycled(holder)
        when (holder) {
            is AutoDisposeViewHolder -> {
                postViewHolderEvent(
                    AutoDisposeViewHolder.ViewHolderEvent.OnUnbindsPosition(
                        holder.adapterPosition
                    )
                )
            }
        }
    }

求大神指导。

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