在底部导航选项卡之间导航时,如何防止 Jetpack Compose 中的 LazyColumn 重置位置?

How to prevent LazyColumn reset position in Jetpack compose when navigating between bottom nav tabs?

@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)
        }
    }
}

每当从底部导航导航到其他屏幕时,惰性列会将滚动位置重置为顶部

Followed this guide to setup the bottom nav

每当您 select BottomNavigationItem 您的可组合项被其他可组合项替换并且当您 select 返回时,它会再次重新初始化。要在 BottomNavigationItem.

中恢复以前的可组合使用 saveState = truerestoreState = true
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
      }
  }
)