我有一个LazyColumn
包含多个LazyRow
. 用旧的术语来说,嵌套的 RecyclerView。
我的问题是,LazyColumn
移动到新的可组合(不同的选项卡)时不会恢复滚动状态。但是 inner LazyRow
s 恢复了它们的状态。
例如,打开主屏幕,滚动到底部然后滚动LazyRow
到结束,然后打开不同的选项卡并再次返回主选项卡。LazyColumn
从顶部开始(不恢复状态),但最后LazyRow
恢复它的滚动状态。
主屏幕包含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 包含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)
}
}
}
如何恢复LazyColumn
滚动状态?