RecyclerView 使用不规范所带来的问题

date
Feb 3, 2021
slug
something error
status
Published
tags
问题
RecyclerView
summary
type
Post
notion image

问题 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 所持有的数据列表不一致造成的。

可能复现的场景

  1. 数据源变动与刷新的 notifyxxxx 相关方法不一致,比如 remove 3 条,但是 notify 仅 1 条
  1. clear 之后,迅速触发滑动事件
  1. Adapter Item 点击事件的内部类使用 final 修饰的 Int 造成的问题

解决方案

  1. 使用 DiffUtils 托管 RecyclerView 的刷新事件
  1. 在有新的替换数据时进行 clear,addAll 操作,后刷新列表
  1. Item 点击事件不写在 onBindViewHolder 中,可放在 onCreateViewHolder 或 ViewHolder 中

参考链接

偶遇RecyclerView内部Bug
不知道你们遇见没有,在RecyclerView被推的如火如荼的时候,你喜欢它,你默默用它,青睐于它的健壮性。你觉得,这玩意儿都出来这么久了,一定没问题。然而,在某一次快速滑动中,Boom,崩溃了! 1 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6). 2 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3300) 3 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3258) 4 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1803) 5 at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1302) 6 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265) 7 at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1093) 8 at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:956) 9 at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:2715) 10 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 11 at android.view.Choreographer.doCallbacks(Choreographer.java:555) 12 at android.view.Choreographer.doFrame(Choreographer.java:524) 13 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 14 at android.os.Handler.handleCallback(Handler.java:615) 15 at android.os.Handler.dispatchMessage(Handler.java:92) 16 at android.os.Looper.loop(Looper.java:137) 17 at android.app.ActivityThread.main(ActivityThread.java:4921) 18 at java.lang.reflect.Method.invokeNative(Native Method) 19 at java.lang.reflect.Method.invoke(Method.java:511) 20 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 21 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 22 at dalvik.system.NativeStart.main(Native ...

© Craig Hart 2021 - 2025