youlookwhat / ByRecyclerView

🔥 RecyclerView 下拉刷新、上拉松手/自动加载更多、item点击/长按、item局部刷新、头布局/尾布局/状态布局、万能分割线、Skeleton骨架图、极简adapter、嵌套滑动置顶
https://youlookwhat.github.io/ByRecyclerView
Apache License 2.0
798 stars 139 forks source link

发现一个bug #63

Closed renhuan closed 1 year ago

renhuan commented 1 year ago

当data数据为空的时候,

            recyclerView.setEmptyView(emptyLayoutId)
            recyclerView.setNotFullScreenNoLoadMore()
            recyclerView.setEmptyViewEnabled(true)
            recyclerView.isLoadMoreEnabled = false //这句会报错
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionSimpleViewHolder{c48e259 position=2 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent} me.jingbin.library.ByRecyclerView{b86ec9a VFED..... ......ID 0,0-1080,1920 #7f080072 app:id/byRecyclerView}, adapter:me.jingbin.library.ByRecyclerView$WrapAdapter@da5d9cb, layout:androidx.recyclerview.widget.LinearLayoutManager@5fbc5a8,
renhuan commented 1 year ago

当data数据为空的时候,

            recyclerView.setEmptyView(emptyLayoutId)
            recyclerView.setNotFullScreenNoLoadMore()
            recyclerView.setEmptyViewEnabled(true)
            recyclerView.isLoadMoreEnabled = false //这句会报错  如果为true不会报错
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionSimpleViewHolder{c48e259 position=2 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent} me.jingbin.library.ByRecyclerView{b86ec9a VFED..... ......ID 0,0-1080,1920 #7f080072 app:id/byRecyclerView}, adapter:me.jingbin.library.ByRecyclerView$WrapAdapter@da5d9cb, layout:androidx.recyclerview.widget.LinearLayoutManager@5fbc5a8,
youlookwhat commented 1 year ago

好的,我这边看看

youlookwhat commented 1 year ago

解决方法: 1、Demo已经提供了ByLinearLayoutManager,可以将LinearLayoutManager替换为它 2、在代码最后加上mAdapter.setNewData(null);

            recyclerView.setEmptyView(emptyLayoutId)
            recyclerView.setNotFullScreenNoLoadMore()
            recyclerView.setEmptyViewEnabled(true)
            recyclerView.isLoadMoreEnabled = false 
            mAdapter.setNewData(null)

可以试试~ @renhuan

renhuan commented 1 year ago

好的,多谢

csh159 commented 1 year ago

isRefreshEnabled = false 设置也是会报上面的错,用ByLinearLayoutManager可能会导致数据错乱,有什么好的办法解决吗?

youlookwhat commented 1 year ago

isRefreshEnabled = false 设置也是会报上面的错,用ByLinearLayoutManager可能会导致数据错乱,有什么好的办法解决吗?

用ByLinearLayoutManager数据错乱应该是其他的问题,不是这个类导致的,建议你检查下数据错乱的愿意

csh159 commented 1 year ago

isRefreshEnabled = false 设置也是会报上面的错,用ByLinearLayoutManager可能会导致数据错乱,有什么好的办法解决吗?

用ByLinearLayoutManager数据错乱应该是其他的问题,不是这个类导致的,建议你检查下数据错乱的愿意

你可以测试下,多种布局情况,加载一页不要滑列表,然后请求上一页数据关闭刷新功能,用ByLinearLayoutManager会导致数据不一致的情况。还发现个问题,刷新的关闭与打开scrollToPosition里面的position不一样。我这么写才是正确的 binding.chatRecyclerView.scrollToPosition(adapter.itemCount - if (binding.chatRecyclerView.isRefreshEnabled) 0 else 1),也就是移到最底部的一个位置,而不能直接写scrollToPosition(adapter.itemCount -1)

youlookwhat commented 1 year ago

就算你用系统的LinearLayoutManager也应该有数据不一致的问题,因为ByLinearLayoutManager就把系统的方法拿过来try了一下而已,而且java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item posit,是官方RecyclerView的bug。设置isRefreshEnabled后要使用 adatper.setNewData()刷新。

第二个是的,scrollToPosition里的position是不一样,实际上下拉刷新头占用一个item。可以这样写:scrollToPosition(adapter.itemCount+recyclerView.getCustomTopItemViewCount()-1)