RecyclerView 使用不规范所带来的问题
date
Feb 3, 2021
slug
something error
status
Published
tags
问题
RecyclerView
summary
type
Post
问题 Log 及相关信息
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 150(offset:150).state:153
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1618)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4702)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
问题原因
数据源与 RecyclerView 所持有的数据列表不一致造成的。
可能复现的场景
- 数据源变动与刷新的 notifyxxxx 相关方法不一致,比如 remove 3 条,但是 notify 仅 1 条
- clear 之后,迅速触发滑动事件
- Adapter Item 点击事件的内部类使用 final 修饰的 Int 造成的问题
解决方案
- 使用 DiffUtils 托管 RecyclerView 的刷新事件
- 在有新的替换数据时进行 clear,addAll 操作,后刷新列表
- Item 点击事件不写在 onBindViewHolder 中,可放在 onCreateViewHolder 或 ViewHolder 中