I have a LazyColumn
that contains multiple LazyRow
. In old terms, nested RecyclerView.
My problem is, LazyColumn
does not restore scroll state when move to a new composable (Different tab). But inner LazyRow
s restore their states.
For example, open home screen, scroll to bottom then scroll LazyRow
to end then open a different tab and back to home tab again. LazyColumn
starts from top (does not restore state) but the last LazyRow
restore it's scroll state.
HomeScreen that contains LazyColumn
@Composable
fun HomeScreen(
homeViewModel: HomeViewModel = hiltViewModel()
) {
val scrollState = rememberLazyListState()
LazyColumn(contentPadding = PaddingValues(vertical = 8.dp),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(8.dp),
state = scrollState,
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background)
) {
items(5) {
TopRatedProducts(homeViewModel = homeViewModel)
}
}
}
TopRatedProducts that contains LazyRow
@Composable
fun TopRatedProducts(
homeViewModel: HomeViewModel = hiltViewModel()
) {
val topRatedProducts by rememberFlowWithLifecycle(homeViewModel.topRatedProducts)
.collectAsState(initial = emptyList())
LazyRow(
contentPadding = PaddingValues(horizontal = 8.dp), // Space between start and end
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp), // Space between items
modifier = Modifier
.background(Color.Green)
) {
items(topRatedProducts) {
ProductCardItem(item = it)
}
}
}
How can I restore LazyColumn
scroll state?