首页 > 解决方案 > NavigationComponent 从 BottomNavitationView 导航

问题描述

我正在使用 NavitationComponent 并且我的开始屏幕有一个BottomNavigationView. 这是在我的活动的 XML 中声明的,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <FrameLayout
        android:id="@+id/bottomNavigationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="105dp"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            app:menu="@menu/bottom_nav_menu" />

    </FrameLayout>

    <!-- Screen contents -->
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationLayout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/home_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我想完全导航到一个没有底部导航视图的单独屏幕。但是,由于它存在于 Activity 上,因此它仍然存在。在这种情况下推荐的方法是什么?我想避免使用startActivity,这就像一开始就违背了拥有导航组件的目的。

标签: androidandroidxandroid-jetpackandroid-jetpack-navigation

解决方案


在企业应用程序中使用带有 Android 导航组件的 BottomNavigation 时,我们面临多个问题:

  1. 与在 Google Samples 中具有解决方法的 BottomNavigation 一起处理返回堆栈。
  2. 使用具有动态菜单更新的共享工具栏
  3. Android BottomNavigation 在不同的片段中隐藏/显示

我们最终得到了这个解决方案:

在我们的 Activity 中,我们为导航组件设置了一个监听器:

findNavController().addOnDestinationChangedListener(this)
override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
        //Check Destination
}

我们为每个片段定义了 UI 配置(工具栏和底部导航):

data class UiConfig(val toolbarConfig: ToolbarConfig, val bottomNavigationConfig: BottomNavigationConfig)

data class ToolbarConfig(val showToolbar: Boolean, val title: String, val menu: Int)

data class BottomNavigationConfig(val showBottomNavigation: Boolean)

object UIConfigs {

    val uiConfigs = mapOf(
            R.id.fragment_id to UiConfig(ToolbarConfig(true, "Title", R.menu.home), BottomNavigationConfig(false)),
            ...
    )

}

在我们的监听器中,当目的地改变时(onDestinationChanged):

when (destination.id) {
    R.id.fragment_id -> {
        //Update UI by accessing uiConfigs and getting the info about fragment
       //hide/show BottomNavigation
      //update your shared Toolbar
      //Any shared updates in possible this way
    }
    else -> throw Exception("No config found for ${destination.label}")
}

推荐阅读