Android Jetpack 导航,宿主片段在另一个宿主片段中
Android Jetpack navigation, host fragment inside another host fragment
我正在努力实现一个简单的设计。一个 activity 带有宿主片段。
问题是,其中一个目的地有一个底部导航栏。
这是一张草图
经过一些研究,我发现最好的做法是使用一个带有主机片段的 Activity。
在我的特殊情况下,底部导航栏不应该在登录和注册片段中可见,只是隐藏它对我来说似乎不合适。
我设法创建了一个带有底部导航栏的 activity 连接 main fragment 到 frag 1, frag 2 或 frag 3,但现在我需要添加 login 片段 和 register 片段 我不确定如何处理导航。
这是没有身份验证片段的应用代码。
<?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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Presentation.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
app:title="Glucose Entries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:theme="@style/ToolbarTheme"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_bar"/>
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
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>
我是否应该将导航栏隐藏在不应该出现的片段上?
此外,我认为另一种解决方案可能是不使用底部导航栏提供的自动导航并创建我自己的操作以从 main fragment 导航到 碎片 1、碎片 2 或 碎片 3
恕我直言,让您的单曲 activity 成为显示和隐藏导航栏的片段的真实来源对我来说听起来不错。
我正在努力实现一个简单的设计。一个 activity 带有宿主片段。
问题是,其中一个目的地有一个底部导航栏。
这是一张草图
经过一些研究,我发现最好的做法是使用一个带有主机片段的 Activity。
在我的特殊情况下,底部导航栏不应该在登录和注册片段中可见,只是隐藏它对我来说似乎不合适。
我设法创建了一个带有底部导航栏的 activity 连接 main fragment 到 frag 1, frag 2 或 frag 3,但现在我需要添加 login 片段 和 register 片段 我不确定如何处理导航。
这是没有身份验证片段的应用代码。
<?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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Presentation.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
app:title="Glucose Entries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:theme="@style/ToolbarTheme"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_bar"/>
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
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>
我是否应该将导航栏隐藏在不应该出现的片段上?
此外,我认为另一种解决方案可能是不使用底部导航栏提供的自动导航并创建我自己的操作以从 main fragment 导航到 碎片 1、碎片 2 或 碎片 3
恕我直言,让您的单曲 activity 成为显示和隐藏导航栏的片段的真实来源对我来说听起来不错。