I have a main/detail UI, where user can select an ...
# compose
o
I have a main/detail UI, where user can select an item in the list, and then details are shown in another box. That box has a scrollable layout inside, and everything is working fine, except the scroll state is shared with all the items in the main list. I figured I should inline the
rememberScrollState
and provide inputs there (selected item in the main list), but now the scroll is not remembered at all, every switch in main list starts with initial position. How do I make scroll state in detail pane be persisted per item in the main list?
z
You’re trying to preserve the scroll position in the detail pane even if a different item is selected in the main pane? I don’t think I’ve seen any apps do that really, are you sure that’s what you need to do? Most apps I’ve seen will reset the detail scroll to top when a different item is selected, since it’s a different item you probably want to see the first information about it first. If you really do want to preserve scroll position though, you just need to remember the detail scroll state from somewhere that won’t be recreated every time the item switches.
o
It’s the other way around. If there are items A and B in the main list, I want it to preserve scroll in details pane for A and B separately, so when switching between A and B details pane is scrolled to where it was left for each item.
Copy code
@Composable
fun AreaDetails(area: Area?) {
    if (area == null) return
    val scrollState: ScrollState = rememberSaveable(area, saver = ScrollState.Saver) { ScrollState(0) }
… and somewhere down in the component …
                modifier = Modifier.verticalScroll(scrollState),
Currently it just resets to zero each time component is called with different
area
parameter
z
Ah I see. What if you wrap everything in
AreaDetails
with
Copy code
key(area) {
  // rest of body
}
And then don’t pass
area
to
rememberSaveable
?
1
Actually I don’t think that is going to work -
rememberSaveable
unregisters its registry entry when skipped. You might need to manage your own saving of detail panes’ state, or at least scroll state. The tricky bit is that the total saved state could grow to be the size of your main list multiplied by the size of whatever state your detail panes are saving, so you might also want to implement some LRU logic.
d
Yeah you need a map of area to scrollstate. I've done a similar thing.