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

时间:2021-05-26 06:56:45

标签: android android-jetpack-compose android-jetpack-navigation

@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

1 个答案:

答案 0 :(得分:1)

每当您选择 BottomNavigationItem 时,您的组合项都会被其他组合项替换,当您选择返回时,它会再次重新初始化。要恢复以前的组合,请在您的 saveState = true 中使用 restoreState = trueBottomNavigationItem

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