eowise / recyclerview-stickyheaders

DEPRECATED. Android library that integrate sticky section headers in your RecyclerView
http://eowise.github.io/recyclerview-stickyheaders
MIT License
961 stars 148 forks source link

java.lang.IndexOutOfBoundsException: invalid position 1. State item count is 1 #15

Closed nathanielwolf closed 9 years ago

nathanielwolf commented 9 years ago

None of my code is hit in this stack trace (see below). I am getting this error when reducing the size of my list from many to 1 with an adapter change. I've traced the source of the issue to the face that when StickyHeaderItemDecoration.drawHeaders line:53 is called, the adapter has a size of 1, parent.mState has a size of 1, but parent.getChildCount() == 2 . The second unwanted view contains data from the previous version of the list that shouldn't be there.

No matter how I notify the RecyclerView of an adapter change (notifyDataSetChanged, notifyItemRangeRemoved) the RecyclerView is out of sync with the adapter when StickyHeaderItemDecoration.drawHeaders is called - the for loop their picks up a child that should not exist.

I have a dozen classes involved in this situation, so before I take the hours required to make a simpler version of this failure, do you have any suggestions?

  java.lang.IndexOutOfBoundsException: invalid position 1. State item count is 1
            at android.support.v7.widget.RecyclerView$Recycler.convertPreLayoutPositionToPostLayout(RecyclerView.java:3316)
            at android.support.v7.widget.RecyclerViewHelper.convertPreLayoutPositionToPostLayout(RecyclerViewHelper.java:9)
            at com.eowise.recyclerview.stickyheaders.HeaderStore.isHeader(HeaderStore.java:72)
            at com.eowise.recyclerview.stickyheaders.StickyHeadersItemDecoration.drawHeaders(StickyHeadersItemDecoration.java:62)
            at com.eowise.recyclerview.stickyheaders.StickyHeadersItemDecoration.onDrawOver(StickyHeadersItemDecoration.java:44)
            at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2266)
            at android.view.View.getDisplayList(View.java:12838)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2930)
            at android.view.View.draw(View.java:13947)
            at android.view.View.getDisplayList(View.java:12838)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1060)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at com.jakewharton.madge.MadgeFrameLayout.drawChild(MadgeFrameLayout.java:85)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1060)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.draw(View.java:13947)
            at android.widget.FrameLayout.draw(FrameLayout.java:467)
            at android.view.View.getDisplayList(View.java:12838)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.draw(View.java:13947)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.draw(ActionBarOverlayLayout.java:509)
            at android.view.View.getDisplayList(View.java:12838)
            at android.view.View.getDisplayList(View.java:12880)
            at android.view.View.draw(View.java:13657)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
            at android.view.View.getDisplayList(View.java:12833)
            at android.view.View.getDisplayList(View.java:12880
leruaa commented 9 years ago

Ok, I'm able to reproduce with minor changes in the samples. I'm now seaching what's wrong.

leruaa commented 9 years ago

I have published a fix at the sonatype snapshot repo. You can test it with the 0.5.1-SNAPSHOT version :

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

dependencies {
    compile 'com.eowise:recyclerview-stickyheaders:0.5.1-SNAPSHOT@aar'
}

Please let me know if it works for you.

nathanielwolf commented 9 years ago

That did the trick! Thank you very much. We have just launched a new version of Couchsurfing with your library included (used in Search Events and My Events). We are grateful .

https://play.google.com/store/apps/details?id=com.couchsurfing.mobile.android

rubengees commented 9 years ago

HI, I get a similar Error and it is not fixed for me with the new Snapshot. Stacktrace:

java.lang.IndexOutOfBoundsException: invalid position -1. State item count is 9
            at android.support.v7.widget.RecyclerView$Recycler.convertPreLayoutPositionToPostLayout(RecyclerView.java:3234)
            at android.support.v7.widget.RecyclerViewHelper.convertPreLayoutPositionToPostLayout(RecyclerViewHelper.java:9)
            at com.eowise.recyclerview.stickyheaders.HeaderStore.isHeader(HeaderStore.java:72)
            at com.eowise.recyclerview.stickyheaders.StickyHeadersTouchListener$SingleTapDetector.findItemHolderUnder(StickyHeadersTouchListener.java:65)
            at com.eowise.recyclerview.stickyheaders.StickyHeadersTouchListener$SingleTapDetector.onSingleTapConfirmed(StickyHeadersTouchListener.java:48)
            at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:276)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

It happens when I delete the first Item, add the same Item again and delete the first item. Moreover it happens only on my Tablet with Android 4.1.1 and not on my Phone running 4.4.

rubengees commented 9 years ago

Okay, I don't know why, but it seems to fix the bug when I set HasFixedSize to true in my recyclerview

oinegue commented 9 years ago

Hi, I just want to remark that the first error is gone. I had an error similar to the first post with 0.5.0:

java.lang.IndexOutOfBoundsException: invalid position 5. State item count is 5

but with 0.5.1-SNAPSHOT, the error is gone.