使用 Android 导航架构组件在导航图中进行简单导航

Simple navigation across navigation graphs with Android Navigation Architecture Component

这是一个简单的导航设置示例,在研究了一段时间后我无法使用导航组件库。

假设我有以下屏幕:

顶部的粘性片段和底部的片段在各自的导航图中,这里是main_activity.xml:

<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">

    <fragment
        android:id="@+id/nav_sticky_top"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_graph_sticky_top" />

    <fragment
        android:id="@+id/navigation_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/nav_sticky_top"
        app:navGraph="@navigation/navigation_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我按 "navigate to sibling fragment" 时,它导航到底部导航图中的兄弟片段,这是正确的,结果:

这是navigation_graph.xml:

<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/navigation_graph.xml"
    app:startDestination="@id/blankFragment1">

    <fragment
        android:id="@+id/blankFragment1"
        android:name="com.example.myapplication.BlankFragment1"
        android:label="fragment_blank_fragment1"
        tools:layout="@layout/fragment_blank_fragment1">

        <action
            android:id="@+id/action_blankFragment1_to_siblingFragment"
            app:destination="@id/siblingFragment" />

    </fragment>

    <fragment
        android:id="@+id/siblingFragment"
        android:name="com.example.myapplication.SiblingFragment"
        tools:layout="@layout/fragment_sibling_fragment" />

</navigation>

现在我想实现 "Navigate to fullscreen fragment" 按钮,它应该导航到一个全屏片段,该片段位于单独的第三个导航图中,并且应该位于粘性片段导航图上方和下方的导航图.如何正确实现这一点?我的意思是 没有 技巧,例如将顶部导航图片段的可见性设置为 GONE 并在底部导航图中导航到全屏片段。

更新:我不再推荐这种方法。对于小型应用程序,它可能适合您,但我 运行 在尝试在不同 NavHostFragments 内托管的不同导航图之间传递数据时遇到了麻烦。我认为最简单的方法是 "hack" 在顶层布局中隐藏片段以允许全屏视图。您可以添加一个 addOnDestinationChangedListener 来侦听您想要全屏显示的目的地,并简单地隐藏所需的片段。

原始答案: 我在我的应用程序中这样做,效果很好。您应该将所有当前片段包装在带有 main_nav_graph.xml 的父片段中。例如:

<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">

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

</androidx.constraintlayout.widget.ConstraintLayout>

那么您的全屏片段导航将如下所示:

<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/main_nav_graph.xml"
    app:startDestination="@id/main_fullscreen">

    <fragment
        android:id="@+id/main_fullscreen"
        android:name="com.example.myapplication.MainFullscreen" >

        <action
            android:id="@+id/action_main_fullscreen_to_fullscreen2"
            app:destination="@id/fullscreen2" />

    </fragment>

    <fragment
        android:id="@+id/fullscreen2"
        android:name="com.example.myapplication.Fullscreen2" />
</navigation>

MainFullscreen 的布局类似于您提供的 ConstraintLayout。只需确保将 app:defaultNavHost="true" 添加到所有子 NavHostFragments。

然后当我想从你的 blankFragment1 导航到 fullscreen2 时,我调用这个:

Navigation.findNavController(activity!!, R.id.main_host_fragment).navigate(R.id.action_main_fullscreen_to_fullscreen2)

G运行ted,如果导航图实际上没有连接,除非位于同一个 activity 中,否则导航图没有多大意义,但我只是喜欢应用程序的漂亮图片图表在导航编辑器中给你 :)