做主頁導航時會用到底部導航欄,Jetpack Compose提供了基礎槽位的佈局Scaffold,使用Scaffold可以構建底部導航欄,例如:

  1. @Composable
  2. fun Greeting(vm: VM) {
  3. val list = listOf("One", "Two", "Three")
  4. var selectedItem = remember {
  5. mutableStateOf(0)
  6. }
  7. val navController = rememberNavController()
  8. Scaffold(bottomBar = {
  9. state.takeIf { it.value }?.let {
  10. BottomNavigation {
  11. list.forEachIndexed { index, label ->
  12. BottomNavigationItem(
  13. label = { Text(text = label) },
  14. selected = index == selectedItem.value,
  15. onClick = { selectedItem.value = index },
  16. icon = {
  17. Icon(
  18. painter = painterResource(id = R.drawable.ic_launcher_foreground),
  19. contentDescription = null
  20. )
  21. })
  22. }
  23. }
  24. }
  25. }) {
  26. NavHost(navController = navController, startDestination = "one") {
  27. composable(route = "one") { PageList(navController, vm) }
  28. composable(route = "detail") { PageDetail(vm) }
  29. }
  30. }
  31. }

這是一個最簡單的Scaffold,其主頁時PageList,顯示一列數字,點選數字後會跳轉到PageDetail頁面。

但是有個很大的問題,就是在跳轉到PageDetail頁面之後,BottomNavigation並沒有隨之消失,於是乎出現了這樣一個奇怪的現象:

為了解決這個問題,可以採用State去控制BottomNavigation的可見性,並將其儲存在ViewModel中。

具體做法是:

1.在ViewModel中建立一個包含Boolean值的LiveData變數state。當state為true時繪製BottomNavigation,為false時不繪製

2.在包含Scaffold頁面中監聽state,並控制BottomNavigation的可見性。

3.在PageList(也就是Scaffold導航的主頁)進入時設定state為true、退出時設定state為false

  1. // ViewModel
  2. class VM: ViewModel() {
  3. private val _state: MutableLiveData<Boolean> = MutableLiveData(true)
  4. val state: LiveData<Boolean> get() = _state
  5. fun setState(status: Boolean) {
  6. _state.postValue(status)
  7. }
  8. }
  9. // MainPage
  10. @Compose MainPage(vm: VM) {
  11. LaunchedEffect(key1 = true) {
  12. vm.setState(true)
  13. }
  14. DisposableEffect(key1 = true) {
  15. onDispose {
  16. vm.setState(false)
  17. }
  18. }
  19. }
  20. // page contains Scaffold
  21. @Composable
  22. fun Greeting(vm: VM) {
  23. // State of BottomNavigation`s visibility
  24. val state = remember { mutableStateOf<Boolean>(true) }
  25. // read the BottomNavigation`s visibility from ViewModel and send to State
  26. vm.state.observeAsState().value?.let { state.value = it }
  27. Scaffold(bottomBar = {
  28. // show / hide BottomNavigation controlled by State
  29. state.takeIf { it.value }?.let {
  30. BottomNavigation {
  31. list.forEachIndexed { index, label ->
  32. BottomNavigationItem(
  33. label = { Text(text = label) },
  34. selected = index == selectedItem.value,
  35. onClick = { selectedItem.value = index },
  36. icon = {
  37. Icon(
  38. painter = painterResource(id = R.drawable.ic_launcher_foreground),
  39. contentDescription = null
  40. )
  41. })
  42. }
  43. }
  44. }
  45. }) {
  46. NavHost(navController = navController, startDestination = "one") {
  47. composable(route = "one") { PageList(navController, vm) }
  48. composable(route = "detail") { PageDetail(vm) }
  49. }
  50. }
  51. }

這種做法的好處是簡單,侵入性低,無需修改系統api也無需自定義view。缺點就是麻煩,需要在導航中的每個主頁都進行設定。


我在StackOverflow上提問時有人回答了另一個辦法。這個辦法是給每個螢幕新增標誌位,來區分是否是導航的主頁,之後再建立BottomNavigation時進行判斷。貼一下:

You need to specify which screens you want to show and which screens you dont want; Otherwise it will show to all the screens inside Scaffold's body (which you have bottomBar). The code below was from my app.

Create a state which observes any destination changes on the navController

Inside when you can put any screens that you want to show navigationBar else just set currentScreen to NoBottomBar

  1. @Composable
  2. private fun NavController.currentScreen(): State<MainSubScreen> {
  3. val currentScreen = remember { mutableStateOf<MainSubScreen>(MainSubScreen.Home) }
  4. DisposableEffect(key1 = this) {
  5. val listener = NavController.OnDestinationChangedListener { _, destination, _ ->
  6. when {
  7. destination.hierarchy.any { it.route == MainSubScreen.Home.route } -> {
  8. currentScreen.value = MainSubScreen.Home
  9. } else -> currentScreen.value = MainSubScreen.NoBottomBar
  10. }
  11. }
  12. addOnDestinationChangedListener(listener)
  13. }
  14. return currentScreen
  15. }

On the Scaffold where you put ur bottomBar

so you can check if currentScreen was NoBottomBar if it was, don't show it

  1. // initialized currentScreeen above
  2. val currentScreen by navController.currentScreen()
  3. Scaffold(
  4. bottomBar = {
  5. if (currentScreen != MainSubScreen.NoBottomBar) {
  6. MainBottomNavigation()
  7. } else Unit
  8. }
  9. ) {
  10. // Your screen
  11. }