I have an App with a List-Detail flow built with Jetpack Compose. I would like to retain the scroll position when going back from the details view to the list view.
Surfaces are exchanged based on the Model state:
@Composable
private fun AppContent() {
val scrollerPosition = ScrollerPosition()
Crossfade(State.currentScreen) { screen ->
Surface(color = MaterialTheme.colors.background) {
when (screen) {
is Screen.List -> ListScreen(scrollerPosition)
is Screen.Details -> DetailsScreen(screen.transaction)
is Screen.New -> NewScreen()
}
}
}
}
ListScreen used to have a VerticalScroller and I was giving it a ScrollerPosition to retain the position after screen changes. However, this solution does not work with AdapterList.
This is how it used to be:
@Composable
private fun TransactionsList(modifier: Modifier, scrollerPosition: ScrollerPosition) {
Box(modifier = modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
VerticalScroller(scrollerPosition = scrollerPosition) {
AdapterList(State.transactions, itemCallback = { transaction ->
TransactionListRow(transaction)
ListDivider()
})
}
}
}
How can I make AdapterList retain the scroll position?
@Composable
private fun TransactionsList(modifier: Modifier, scrollerPosition: ScrollerPosition) {
Box(modifier = modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
AdapterList(State.transactions, itemCallback = { transaction ->
TransactionListRow(transaction)
ListDivider()
})
}
}