[英]Android Jetpack navigation: One activity with several navigation views
I am considering the new android jetpack navigation best practices where there should only be one activity in each application.我正在考虑新的android jetpack 导航最佳实践,其中每个应用程序中应该只有一个活动。 I want to use one activity to implement the following menus in fragments, but I'm very insecure about what the best practices are when implementing several menus in the same application.
我想使用一个活动在片段中实现以下菜单,但我对在同一应用程序中实现多个菜单时的最佳实践非常不安全。
BottomNavigationView
( Reference repo ): BottomNavigationView
( 参考BottomNavigationView
):
<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="com.example.android.navigationadvancedsample.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<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_nav"/>
</LinearLayout>
DrawerLayout ( Reference repo ): DrawerLayout(参考仓库):
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/garden_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_garden"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
style="@style/Widget.MaterialComponents.NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation"/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
Considering these examples from google there are no clear answers as of how to implement both views in one activity using best practices, because each example only uses one form of navigation view for their app, respectively DrawerLayout
, or BottomNavigationView
.考虑到来自 google 的这些示例,对于如何使用最佳实践在一个活动中实现两个视图没有明确的答案,因为每个示例仅为其应用程序使用一种形式的导航视图,分别为
DrawerLayout
或BottomNavigationView
。
The questions are: 1. What are the best practices for making activity with different navigation views for different fragments?问题是: 1. 为不同的片段制作具有不同导航视图的活动的最佳实践是什么? For example, several fragments using
DrawerLayout
, and other fragments using BottomNavigationView
.例如,使用
DrawerLayout
几个片段,以及使用BottomNavigationView
其他片段。 2. What if one wants to hide both the DrawerLayout
and the BottomNavigationView
when viewing authentication views and logging in, and display them when authenticated? 2.如果想在查看认证视图和登录时隐藏
DrawerLayout
和BottomNavigationView
,并在认证时显示它们怎么办?
您可以同时实现它们,并且可以以编程方式隐藏或显示它们,或者在您的 Activity 之前拥有一个身份验证视图,该视图包含不<include/>
这些导航组件的导航。
With Jetpack Navigation, you can add a Navigation Host and a Navigation Drawer connected with a main nav graph in the main Activity, and another Navigation Host with a Bottom Navigation connected to a second nav graph inside the default starting fragment.使用 Jetpack Navigation,您可以添加一个 Navigation Host 和一个与主 Activity 中的主导航图相连的 Navigation Drawer,以及另一个带有底部导航的 Navigation Host,其连接到默认起始片段内的第二个导航图。 I found this tutorial https://proandroiddev.com/android-jetpack-navigationui-a7c9f17c510e and its example project https://github.com/sagar-viradiya/NavigationUIDemo
我找到了本教程https://proandroiddev.com/android-jetpack-navigationui-a7c9f17c510e及其示例项目https://github.com/sagar-viradiya/NavigationUIDemo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.