user5099833
user5099833

Reputation:

Save / Restore state of NestedScrollView with multiple RecyclerViews and other views

Update: This method works, only the saving didn't work, so I chose another way to save this array.

In my layout I have a NestedScrollView and in it a LinearLayout, which contains multiple RecyclerViews and other views. The layout is inside a Fragment. I try to save and restore the scroll state the following way (it's Kotlin, so don't wunder about the syntax):

fun NestedScrollView.savePosition(bundle: Bundle?) {
    bundle?.putIntArray("SCROLL_VIEW_POSITION", intArrayOf(scrollX, scrollY))
}

fun NestedScrollView.restorePosition(bundle: Bundle?) {
    bundle?.getIntArray("SCROLL_VIEW_POSITION")?.let { post { scrollTo(it[0], it[1]) } }
}

I call the first function in onSaveInstanceState and the second one after all RecyclerViews have loaded their contents.

But it's not working...

Any ideas?

Upvotes: 3

Views: 1192

Answers (1)

tadiuzzz
tadiuzzz

Reputation: 146

If you want to restore scroll position of NestedScrollView after pop back stack with fragment manager, you don't need to do it manually. For me the problem was in lack of id of NestedScrollView. After I added id in xml layout - everithing works well.

Upvotes: 5

Related Questions