【问题标题】:Android navigation component child navigationAndroid导航组件子导航
【发布时间】:2019-10-09 17:20:04
【问题描述】:

我正在一个新的 Android 应用上使用导航组件,但我不知道该怎么做

首先,我有我的 MainActivity,我有 main_navigation_graph

主要活动 NavHostFragment

<fragment
    android:id="@+id/main_navigation_host_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/main_navigation_graph"
    app:defaultNavHost="true"/>

main_navigation_graph里面有3个片段

这里一切正常。问题是当我到达最后一个片段时,因为在这个片段上,我想根据 BottomNavigationView 输入(暂时)显示一些子片段(在新的 NavHostFragment 内)

最后一个片段:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF">

<fragment
    android:id="@+id/dash_board_navigation"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/dash_board_bottom_navigation"
    app:navGraph="@navigation/dash_board_navigation_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/dash_board_bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:menu="@menu/main_menu_navigation"
    app:labelVisibilityMode="labeled"
    android:background="@color/main_menu_background"
    app:itemIconTint="@color/main_menu_item_color"
    app:itemTextColor="@color/main_menu_item_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

dash_board_navigation_graph

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dash_board_navigation_graph"
    app:startDestination="@id/destination_home_fragment">

    <action
        android:id="@+id/dash_board_action1"
        app:destination="@id/destination_fragment1"/>

    <action
        android:id="@+id/dash_board_action2"
        app:destination="@id/destination_fragment2"/>

    <action
        android:id="@+id/dash_board_action3"
        app:destination="@id/destination_fragment3"/>

    <action
        android:id="@+id/dash_board_action4"
        app:destination="@id/destination_fragment4"/>

    <fragment
        android:id="@+id/destination_fragment1"
        android:name="com.....Fragment1">
    </fragment>

    <fragment
        android:id="@+id/destination_fragment2"
        android:name="com.....Fragment2">
    </fragment>

    <fragment
        android:id="@+id/destination_fragment3"
        android:name="com.....Fragment3">
    </fragment>

    <fragment
        android:id="@+id/destination_fragment4"
        android:name="com.....Fragment4">
    </fragment>

当我从最后一个片段触发这些操作时:

findNavController().navigate(R.id.dash_board_action1/2/3)

我得到了这个例外:

java.lang.IllegalArgumentException: navigation destination com.asperitass.mobile:id/dash_board_home_action is unknown to this NavController

我的问题是:我的方法对吗?或者这(具有多级导航)甚至可能吗?或者我应该使用childFragmentManager 来处理这个子导航并在我的片段中膨胀一个 FrameLayout 吗?

【问题讨论】:

    标签: android navigation


    【解决方案1】:

    据我了解,您正在嵌套两个 NavHostFragment,因为您希望在 lastFragment 登录等流程之后只拥有 bottomNav,

    据我了解,问题出在findNavController().navigate(R.id.dash_board_action1/2/3)。由于您有两个 NavHostFragment,因此您还将有两个相应的 NavController。

    默认的Navigation.findNavController() 将为您提供顶级 NavController,但要在子 navGraph 中导航,您需要子 NavController,您可以通过

    val fragmentContainer = view.findViewById<View>(R.id.dash_board_navigation)
    navController         = Navigation.findNavController(fragmentContainer)
    

    有关更多详细信息,请查看这个非常好的 Stackoverflow 答案https://stackoverflow.com/a/51500083/6269691

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多