Expanding parent view holder -> crash
See original GitHub issueHi guys,
I have following set up: In a support dialog fragment I have a simple RecyclerView where the parent views should be expandable. When I press the button to expand the children the app crashes with:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{24819493 position=19 id=-1, oldPos=6, pLpos:6 scrap [attachedScrap] tmpDetached no parent} at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4487) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4618) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4599) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2954) at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2594)
What I noticed is, that the crash occurs at position 19, but my children list contains only 18 elements. The children list size is for each parent different.
I’m using com.android.support:recyclerview-v7:23.2.1
If you need any additional information, please let me know. Your help is appreciated 😃 Kind regards, Robert Hilse
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:12 (5 by maintainers)
Top GitHub Comments
Just figured it out.
I was going through the adapter code to tidy it up a bit to post a gist here when I realise I still had the getItemCount method from my previous default non expandable implementation.
As it was still overriding the parents getItemCount it was ok to override (no compilation errors) but of course it was not taking the sub items into consideration.
I would strongly suggest to make getItemCount in ExpandableRecyclerAdapter.java as final so we cannot override it. It is a silly thing but I am sure most of us start with a default implementation and then look how to achieve the expandable behaviour.
Hope this helps someone else too!
Added a note to the docs for now, will see if more issues like this roll in throughout the lifetime of the library.