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

IndexOutOfBoundsException when deleting the last item #8

Closed rubengees closed 9 years ago

rubengees commented 9 years ago

Hi, with the latest version (0.4.3) I get a IndexOutOfBoundsException (Invalid index 0, size is 0) when I delete the last item from my Adapter.

This is the code I use:

public void deleteVocable(Vocable vocable) {
        for (int i = 0; i < data.size(); i++) {
            if (vocable.getId() == data.get(i).getId()) {
                data.remove(i);
                this.notifyItemRemoved(i);
                break;
            }
        }
    }
eneim commented 9 years ago

I got the same errors. In my case, I delete all items at once and add a new items set. What I'm thinking of is that your stickyheaderadapter lacks of a notifyDataSetChange call?

rubengees commented 9 years ago

I have discovered that there are two different bugs. The first is the issue above. Another appears when you call

notifyItemRangeRemoved(i, 1);

instead. Moreover i changed some things and now I get the following error when i delete an item and call notifyItemRemoved():

java.lang.IndexOutOfBoundsException: invalid position 2. State item count is 2
            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.StickyHeadersItemDecoration$HeaderStore.isHeader(StickyHeadersItemDecoration.java:164)
            at com.eowise.recyclerview.stickyheaders.StickyHeadersItemDecoration.onDrawOver(StickyHeadersItemDecoration.java:54)

I don't know if my code is that one which contains a bug, but i checked everything twice and couldn't find anything.

leruaa commented 9 years ago

I am about to commit a fix for the first bug.

For the second one, are you able to reproduce it with the samples ?

eneim commented 9 years ago

I found this link, hope this helps!

https://code.google.com/p/android/issues/detail?id=77846

leruaa commented 9 years ago

I've published the 0.4.4-SNAPSHOT version on sonatype. Could you try if you still have an exception ?

rubengees commented 9 years ago

I can't test it. Could you publish it, so I can build with Gradle? (Using Android Studio)

leruaa commented 9 years ago

You can add the sonatype snapshot repo to your build.gradle :

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

and change the recyclerview-stickyheader lib version :

dependencies {
    compile 'com.eowise:recyclerview-stickyheaders:0.4.4-SNAPSHOT@aar'
}
rubengees commented 9 years ago

I still get an error: Failed to find

I added everything you suggested above.

leruaa commented 9 years ago

What's the error exactly ?

Can you provide a PoC that reproduce it ?

rubengees commented 9 years ago

Sorry I made a mistake, testing now.

rubengees commented 9 years ago

Okay, removing items looks good now, but I'm getting strange errors calling onNotifyDataSetChanged() when I add items. I get the following Error:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.add(ArrayList.java:147)
            at com.eowise.recyclerview.stickyheaders.HeaderStore.isHeader(HeaderStore.java:74)
            at com.eowise.recyclerview.stickyheaders.StickyHeadersItemDecoration.getItemOffsets(StickyHeadersItemDecoration.java:89)
            at android.support.v7.widget.RecyclerView.getItemDecorInsetsForChild(RecyclerView.java:2662)
            at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:5434)

I can't use notifyOnItemInserted() because I don't know where the new items are, because I sort my List after insertion.

leruaa commented 9 years ago

Ok, can you make another test after replacing 0.4.4-SNAPSHOT with 0.5.0-SNAPSHOT in your build.gradle ? I think this issue is fixed by PR #13, witch wasn't included in 0.4.4-SNAPSHOT.

rubengees commented 9 years ago

Ok, it works! Thank you for your help!

leruaa commented 9 years ago

Finally ! ;)

rubengees commented 9 years ago

Haha great