jmartinesp / SpannedGridLayoutManager

Android RecyclerView.LayoutManager that resizes and reorders views based on SpanSize
MIT License
600 stars 105 forks source link

NoSuchElementException on screen rotation #10

Closed jaumard closed 7 years ago

jaumard commented 7 years ago

Don't know if it's related to your code or mine as I don't manage to reproduce it on the sample app but here is what happen to me.

I load items from network and put them into my adapter, no problem they appear, I rotate the screen it goes in landscape without problem and go back too.

But if in landscape I scroll down the list and then want to go back to portrait then it crash with :

java.util.NoSuchElementException: Collection contains no element matching the predicate.
                                                                        at com.arasthel.spannedgridlayoutmanager.SpannedGridLayoutManager.onSaveInstanceState(SpannedGridLayoutManager.kt:954)
                                                                        at android.support.v7.widget.RecyclerView.onSaveInstanceState(RecyclerView.java:1270)
                                                                        at android.view.View.dispatchSaveInstanceState(View.java)
                                                                        at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java)
                                                                        at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(RecyclerView.java:1297)
                                                                        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java)
                                                                        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java)
                                                                        at android.view.View.saveHierarchyState(View.java)
                                                                        at android.support.v4.app.FragmentManagerImpl.saveFragmentViewState(FragmentManager.java:2849)
                                                                        at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:2870)
                                                                        at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:2923)
                                                                        at android.support.v4.app.FragmentController.saveAllState(FragmentController.java:125)
                                                                        at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:528)
                                                                        at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:509)
                                                                        at android.app.Activity.performSaveInstanceState(Activity.java)
                                                                        at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java)
                                                                        at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java)
                                                                        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java)
                                                                        at android.app.ActivityThread.-wrap19(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
                                                                        at android.os.Handler.dispatchMessage(Handler.java)
                                                                        at android.os.Looper.loop(Looper.java)
                                                                        at android.app.ActivityThread.main(ActivityThread.java)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

I made a quick video to show you the problem. Any idea ?

device-2017-10-31-175406.mp4.zip

jmartinesp commented 7 years ago

This is a bug related to items not keeping their order (layout index) in the LayoutManager and trying to restore scroll position. I've added a workaround and a itemOrderIsStable flag that must be set to true to be restore scroll position.

jmartinesp commented 7 years ago

Should be fixed in version 2.0.1.

jaumard commented 7 years ago

Nice that was quick fixes thanks ! :D I'll try to test this today ! Does this mean that in my case it's not possible to restore the scroll position ? My understanding of how it should work is you take the first one visible and when you try restore the position you "just" manage to show the first one that was visible on the screen and nothing more. Like this the should be ok for all rotation even if the number of span is not the same in different rotation.

jaumard commented 7 years ago

No more crash ! Perfect now :)