gpt4 book ai didi

java - 带有 NavigationView 的嵌套导航图

转载 作者:行者123 更新时间:2023-11-30 06:01:05 25 4
gpt4 key购买 nike

我的场景就像我有一个包含(登录/注册 fragment )的 MainActivity。我有一个 HomeActivity(其中包含带有抽屉布局的导航 View 。)

这两个 Activity 都有各自独立的导航图。当我在 main_navigation_graph 中包含 home_navigation_graph 并导航到它时,DrawerLayout 不起作用。但是当我单独做这件事时,就像没有嵌套只调用 home_navigation_graph(HomeActivity) 作为启动器一样,一切正常。这是我的示例代码:

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

<fragment
android:id="@+id/loginFragment"
android:name="com.example.app.ui.login.LoginFragment"
android:label="LoginFragment"
tools:layout="@layout/fragment_login">
<action
android:id="@+id/action_loginFragment_to_signupFragment"
app:destination="@id/signupFragment" />
<action
android:id="@+id/action_loginFragment_to_mobile_navigation"
app:destination="@id/mobile_navigation"
app:launchSingleTop="true"
app:popUpTo="@+id/nav_graph_login"
app:popUpToInclusive="true" />
</fragment>

<fragment
android:id="@+id/signupFragment"
android:name="com.example.app.ui.signup.SignupFragment"
android:label="SignupFragment"
tools:layout="@layout/fragment_signup">
<action
android:id="@+id/action_signupFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:launchSingleTop="true"
app:popUpTo="@id/signupFragment"
app:popUpToInclusive="true" />
</fragment>

<include app:graph="@navigation/mobile_navigation" />
</navigation>

当我在单击登录按钮时调用第二个移动导航图(使用代码 belwo)时,HomeActivity 打开但只有 homeDestinationFragment 是可见的,其余没有任何作用

findNavController().navigate(R.id.action_loginFragment_to_mobile_navigation)

下面的代码是 home_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/mobile_navigation"
app:startDestination="@+id/nav_home">

<fragment
android:id="@+id/nav_home"
android:name="com.example.app.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />

<fragment
android:id="@+id/nav_gallery"
android:name="com.example.app.ui.earnings.VendorEarningFragment"
android:label="@string/menu_vendor_earning"
tools:layout="@layout/fragment_vendor_earning" />

<fragment
android:id="@+id/nav_slideshow"
android:name="com.example.app.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />

<fragment
android:id="@+id/nav_tools"
android:name="com.example.app.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />

<fragment
android:id="@+id/nav_share"
android:name="com.example.app.ui.share.ShareFragment"
android:label="@string/menu_share"
tools:layout="@layout/fragment_share" />

<fragment
android:id="@+id/nav_send"
android:name="com.example.app.ui.send.SendFragment"
android:label="@string/menu_send"
tools:layout="@layout/fragment_send" />
</navigation>

请让我知道我缺少什么。我被困在这几个星期了。

最佳答案

在您的 home_navigation_graph 中只包含抽屉布局和导航菜单而不是整个图表。它对我来说效果很好,您可以查看我的 github 存储库。

替换下面几行

 <include app:graph="@navigation/mobile_navigation" />

下面是这段代码

<activity
android:id="@+id/mainActivity"
android:name="com.example.nav_experiment.MainActivity"
android:label="activity_main"
tools:layout="@layout/activity_main" />

在下面的 Git 链接中有抽屉导航,它在登录按钮单击时打开,根据查询要求

详细说明按照 git 链接 -> https://github.com/gautammittal23/NaV_Graph_With_login_navigation_drawer

关于java - 带有 NavigationView 的嵌套导航图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58993504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com