NavigationComponent导航离开BottomNavitationView

问题描述 投票:0回答:1

我正在使用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>

我想完全导航到一个单独的屏幕,该屏幕不具有BottomNavigationView。但是,由于它存在于活动中,因此它仍然存在。在这种情况下,推荐的方法是什么?我想避免使用startActivity,这就像打败了首先拥有Navigation Component的目的一样。

android androidx android-jetpack android-jetpack-navigation
1个回答
0
投票

[在企业应用程序中将BottomNavigation与Android导航组件一起使用时,我们面临多个问题:

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

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

在我们的活动中,我们为导航组件设置了一个侦听器:

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

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

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}")
}
© www.soinside.com 2019 - 2024. All rights reserved.