@Composable
fun HomeScreen(){
val popularListState = rememberLazyListState()
val viewModel: HomeViewModel = hiltNavGraphViewModel()
val popularMovies: List<Movie> by viewModel.popularMoviesLiveData.observeAsState(emptyList())
LazyColumn(
state = popularListState
) {
itemsIndexed(movies) { _, movie ->
HomeMovieComponent(movie = movie)
}
}
}
每当从底部导航导航到其他屏幕时,惰性列会将滚动位置重置为顶部
答案 0 :(得分:1)
每当您选择 BottomNavigationItem
时,您的组合项都会被其他组合项替换,当您选择返回时,它会再次重新初始化。要恢复以前的组合,请在您的 saveState = true
中使用 restoreState = true
和 BottomNavigationItem
。
BottomNavigationItem(
icon = { /*Some Icon*/ },
label = { /*Some Text*/ },
selected = currentRoute == item.route,
onClick = {
navController.navigate(item.route) {
// Put These line in your code.
popUpTo(navController.graph.startDestinationId) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
}
)