Android Jetpack导航,另一个主机片段内的主机片段

Posted

技术标签:

【中文标题】Android Jetpack导航,另一个主机片段内的主机片段【英文标题】:Android Jetpack navigation, host fragment inside another host fragment 【发布时间】:2019-05-29 03:26:08 【问题描述】:

我正在尝试实现一个简单的设计。一个带有宿主片段的活动。

问题是,其中一个目的地有一个底部导航栏。

这是一幅素描

经过一番研究,我发现最好的做法是拥有一个带有宿主片段的 Activity。

在我的特殊情况下,底部导航栏在登录和注册片段中不应该是可见的,只是隐藏它对我来说似乎不合适。

我设法创建了一个底部导航栏的活动,将 主片段 连接到 frag 1frag 2frag 3 ,但是现在我需要添加登录片段注册片段,我不知道如何处理导航。

这是没有身份验证片段的应用程序代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_
    android:layout_
    android:orientation="vertical"
    tools:context=".Presentation.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="Glucose Entries"
        android:layout_
        android:layout_
        android:background="?colorPrimary"
        android:theme="@style/ToolbarTheme"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_
        android:layout_
        app:menu="@menu/bottom_navigation_bar"/>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/mobile_navigation"
        app:defaultNavHost="true" />

</LinearLayout>

还有:

class MainActivity : AppCompatActivity() 

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        navController = Navigation.findNavController(this, R.id.nav_host_fragment)
        bottom_nav.setupWithNavController(navController)
        NavigationUI.setupActionBarWithNavController(this, navController)
    

    // stuff

还有导航文件:

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

    <!--frag 1 -->
    <fragment
        android:id="@+id/mainListFragment"
        android:name="com.gluco.Presentation.MainList.MainListFragment"
        android:label="Glucose Entries"
        tools:layout="@layout/main_list_fragment">
    </fragment>

    <!--frag 2 -->
    <fragment
        android:id="@+id/statisticsFragment"
        android:name="com.gluco.Presentation.Statistics.StatisticsFragment"
        android:label="statistics_fragment"
        tools:layout="@layout/statistics_fragment" />

    <!--frag 3 -->
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.gluco.Presentation.Settings.SettingsFragment"
        android:label="SettingsFragment" />

</navigation>

我应该把导航栏隐藏在不应该在的片段上吗?

另外,我认为另一种解决方案可能是不使用底部导航栏提供的自动导航,而是创建我自己的操作来从 ma​​in fragment 导航到 frag 1frag 2frag 3

【问题讨论】:

你能提出你的解决方案吗?即使我被困在同一个问题上。 我最终做的是在主活动中使用一个带有底部导航栏的导航主机片段。然后我会根据我所在的片段隐藏栏。为了决定应用程序启动时的导航位置,我使用了一个简单的 if 语句。检查这个:this,它在某种程度上相关。这是该项目的link,这是一个学校项目,因此您可能会发现其他小问题 当你遇到这样的情况时,你有 NavHost 在另一个里面,比如底部导航,点击底部导航项目中的任何项目,显示一个没有底部导航的新屏幕,创建具有***目的地的新图表,并使用带有导航控制器的活动来设置目的地。如果不清楚,请告诉我,我将分享详细答案作为评论。 【参考方案1】:

恕我直言,让您的单一活动成为关于哪个片段显示和隐藏导航栏的事实来源对我来说听起来不错。

【讨论】:

除此之外,想链接this,特别是丹麦汗的回答

以上是关于Android Jetpack导航,另一个主机片段内的主机片段的主要内容,如果未能解决你的问题,请参考以下文章

使用 Jetpack 的 Android 导航组件销毁/重新创建的片段

android jetpack 导航仪器测试在返回导航上失败

Android Jetpack Navigation、BottomNavigationView 与 Youtube 或 Instagram 类似正确的后退导航(片段后退堆栈)?

如何在使用Android Jetpack导航时禁用导航图标

Jetpack导航。改变每个片段的工具栏按钮

使用jetpack导航组件从一个底页导航到另一个底页时如何弹出底页对话框