I have a LazyColumn
with nested LazyRows
(similar to Netflix or Spotify home page feed). In short, the issue I'm having is that when the content of the page changes, the scrolling positions of the nested LazyRows are not reset.
For example, user scrolls down the home page, scrolls the 3rd horizontal sections to see the 5th item, refreshes the home page, the new page content is loaded. But the 3rd horizontal section still shows the 5th instead of resetting to the initial position.
In terms of code, here's some snippets:
In HomeScreen composable:
Scaffold {
when (val viewState = viewModel.stateFlow.collectAsState().value) {
is ViewState.Success -> {
Box {
with(viewState.data) {
LazyColumn {
itemsIndexed(sections) { index, section ->
LazyRow{}
//etc.
}
}
}
}
}
In the HomeViewModel, when user refreshes home screen this function is called:
private val _stateFlow = MutableStateFlow<ViewState<HomePage>>(ViewState.Loading)
val stateFlow: StateFlow<ViewState<HomePage>>
get() = _stateFlow
fun loadHomeScreen() {
viewModelScope.launch {
repository.getHomePage()
.collect {
_stateFlow.tryEmit(it)
}
}
}
Now I have managed to add extra code to scrolls the home page's LazyColumn to the top of the screen when user refreshes the home screen. However, I still don't know what's the best way to do that for the nested LazyRows. I do not want to keep a reference to their LazyListState
as that solution does not scale well. However, I am confused on why the scrolling position is not being reset when new state is emitted and new LazyRows are displayed. Is that a bug in Compose? Or am I missing something?