简体   繁体   English

如何使用嵌套的 navGraph 和底部导航视图进行导航

[英]How to navigate with nested navGraph and bottom navigation view

I've bottom nav view with 3 item, My navGraph looks like this:我有 3 个项目的底部导航视图,我的navGraph看起来像这样:

<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"
app:startDestination="@id/nested_navigation"

<navigation
    android:id="@+id/nested_navigation"
    app:startDestination="@id/mainFragment" >
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.app.ui.main.MainFragment"
        android:label="main_fragment"
        tools:layout="@layout/main_fragment" />
    <fragment
        android:id="@+id/list"
        android:name="com.example.app.ui.main.List"
        android:label="fragment_news_list"
        tools:layout="@layout/fragment_list" />
</navigation>

<fragment
    android:id="@+id/settings"
    android:name="com.example.app.ui.main.Settings"
    android:label="Settings" />
</navigation>

The navigation in the bottom navigation view with the nested navGraph fragments work properly, but if I navigate to settings_fragment , which is outside the nested navGraph, And I click on the other items/fragments I can't navigate to the other fragments and I basically stuck on this screen.带有嵌套 navGraph 片段的底部导航视图中的导航可以正常工作,但是如果我导航到嵌套 navGraph 之外的settings_fragment ,然后单击其他项目/片段,我将无法导航到其他片段,我基本上卡在这个屏幕上。

I checked what happened is if I put the settings_fragment inside the nested navGraph, and it's works great.我检查了如果我将settings_fragment放在嵌套的 navGraph 中发生的事情,它的效果很好。

How can I fix this problem?我该如何解决这个问题?

btw - I'm pretty sure it's not related, but settings fragment is PreferenceScreen layout that sits inside XML resource and not layout resource顺便说一句 - 我很确定它不相关,但设置片段是位于 XML 资源而不是布局资源内的PreferenceScreen布局

My menu items:我的菜单项:

<item
    android:id="@+id/mainFragment"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/home"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/list"
    android:icon="@drawable/ic_format_list_bulleted_black_24dp"
    android:title="@string/news_list"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_settings_black_24dp"
    android:title="@string/settings"
    app:showAsAction="ifRoom"
    />

The issue is to do with the structure of your nav graph.问题与导航图的结构有关。

Bottom navigation will only take into account the root elements.底部导航将仅考虑根元素。

- nested_navigation (root element) defaults to `mainFragment`
 |- mainFragment (child element)
 |- list (child element)
- settings (root element)

So given the above illustration, you will only be able to make use of the bottom navigation to navigate between settings and nested_navigation which in turn would be mainFragment .因此,鉴于上图,您将只能使用底部导航在settings和嵌套导航之间导航,而mainFragment nested_navigation

If you were to navigate between settings and list it would not have been possible.如果您要在settingslist之间导航,这是不可能的。

Please take note that the id of the menu items have to match the id of the graph destination.请注意,菜单项的id必须与图形目标的id匹配。

Eg例如

<item
    android:id="@+id/nested_navigation"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/home"
    app:showAsAction="ifRoom" />

<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_settings_black_24dp"
    android:title="@string/settings"
    app:showAsAction="ifRoom" />

Note the id of the two elements match precisely the id of the root destinations.请注意,两个元素的idroot目的地的id完全匹配。

Extra额外的

Perhaps my other answer may be of help to complement the navigation flow -> How to switch to other fragment in different back stack using Navigation Component?也许我的其他答案可能有助于补充导航流程->如何使用导航组件切换到不同后台堆栈中的其他片段?

Bottom navigation will only take into account the root elements.底部导航将仅考虑根元素。 You can rename the item in menu same as in navigation graph.您可以像在导航图中一样重命名菜单中的项目。

EX.前任。 Your nested graph name is homeNavigation -> let's name id in menu is homeNavigation.您的嵌套图形名称是 homeNavigation -> 让我们在菜单中命名 id 是 homeNavigation。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM