CardStackView icon indicating copy to clipboard operation
CardStackView copied to clipboard

Very fast scrolling in some cases throws IndexOutOfBoundsException

Open panekzogen opened this issue 7 years ago • 4 comments

Very fast scrolling with updating items can produce following exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: future.company.beside, PID: 15159
    java.lang.IndexOutOfBoundsException: Invalid item position 3(3). Item count:3 future.company.beside.ext.cardstackview.CardStackView{150412c VFED..... ......ID 32,32-1048,1436 #7f0a004b app:id/cardStack}, adapter:future.company.beside.component.card.CardStackAdapter@70f57f5, layout:future.company.beside.ext.cardstackview.CardStackLayoutManager@ad3498a, context:future.company.beside.MainActivity@3609d2f
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5885)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at future.company.beside.ext.cardstackview.CardStackLayoutManager.update(CardStackLayoutManager.java:222)
        at future.company.beside.ext.cardstackview.CardStackLayoutManager.scrollHorizontallyBy(CardStackLayoutManager.java:70)
        at android.support.v7.widget.RecyclerView.scrollStep(RecyclerView.java:1829)
        at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5067)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:693)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

As i understand this situation, new item hasn't been processed yet but was added in adapter. Happens when adapter.itemCount < visibleCount, in this situation in update method loop working rule is i < getItemCount()

for (int i = state.topPosition; i < state.topPosition + setting.visibleCount && i < getItemCount(); i++) {
...
}

Solution: Provide to update method RecyclerView.State which has getItemCount(). This count will be updated when new item will be measured.

panekzogen avatar Mar 03 '19 22:03 panekzogen

I have the same issue.How to solve it?

plhbao avatar May 08 '19 04:05 plhbao

@taomWTP how did you resolve the issue?

richanshah avatar May 03 '21 05:05 richanshah

@yuyakaido why you close the issue without having solution ?

richanshah avatar May 03 '21 05:05 richanshah

@richanshah did you find any solution??

PMARZV avatar Aug 13 '23 23:08 PMARZV