【问题标题】:Android Navigation Component - How to have bottom navigation apply to only some fragmentsAndroid导航组件 - 如何让底部导航仅适用于某些片段
【发布时间】:2021-08-13 21:44:54
【问题描述】:

我正在开发一个 Android 应用程序 (java),它目前只有一个活动 (MainActivity),它通过底部导航加载四个片段。我正在使用导航组件,这是 nav_graph:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
android:id="@+id/nav_graph"
app:startDestination="@id/homeFragment">

<fragment
    android:id="@+id/homeFragment"
    android:name="com.myapp.app.HomeFragment"
    android:label="Home"
    tools:layout="@layout/fragment_home">
    // .. actions to sub fragments ..
</fragment>
<fragment
    android:id="@+id/secondFragment"
    android:name="com.myapp.app.SecondFragment"
    android:label="Second"
    tools:layout="@layout/fragment_second" />
    // .. actions to sub fragments ..
<fragment
    android:id="@+id/thirdFragment"
    android:name="com.myapp.app.ThirdFragment"
    android:label="Third"
    tools:layout="@layout/fragment_third">
    // .. actions to sub fragments ..
</fragment>
<fragment
    android:id="@+id/fourthFragment"
    android:name="com.myapp.app.FourthFragment"
    android:label="Fourth"
    tools:layout="@layout/fragment_fourth" >
    // .. actions to sub fragments ..
</fragment>

// .. More subfragments linked from the first four ..


</navigation>

我希望有一个单独的流程,其中有一个入职屏幕,可以链接到登录或注册。登录或完成注册过程后,用户应返回带有底部导航的主页片段。问题是,整个第二个流程不应该显示工具栏或底部导航,它们应该是全屏的,并且不应该允许访问底部导航布局。

如何让底部导航只影响主应用中的那些片段,而不影响登录/注册过程中的屏幕?

【问题讨论】:

    标签: android android-fragments android-navigation


    【解决方案1】:

    您可以在 MainActivity 的 oncreate() 中使用 NavController 的“addOnDestinationChangedListener”方法。这是用 kotlin 编写的示例代码。

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
            when (destination.id) {
                R.id.splashFragment, R.id.welcomeFragment, R.id.signInFragment, R.id.signUpFragment, R.id.forgotPasswordFragment,
                R.id.forgotPasswordStepTwoFragment -> {
                    mToolbar.visibility = View.GONE
                    navViewBottom.visibility = View.GONE
                }
                R.id.homeFragment -> {
                    mToolbar.visibility = View.VISIBLE
                    navViewBottom.visibility = View.VISIBLE
                }
    
                else -> {
                    mToolbar.visibility = View.VISIBLE
                    navViewBottom.visibility = View.VISIBLE
                }
            }
    

    为此,您必须在 MainActivity 类中声明 NavController。

        private lateinit var navController: NavController
        navController = findNavController(R.id.nav_host_fragment)
    

    这里的“nav_host_fragment”是您的 xml 文件中的片段容器。 希望它有效...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2018-03-10
      • 2020-03-23
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多