繁体   English   中英

如何同时使用导航抽屉和底部导航 - 导航架构组件

[英]How to use Navigation Drawer and Bottom Navigation simultaneously - Navigation Architecture Component

我有如下屏幕,其中在同一屏幕上包含导航抽屉和底部导航:

应用程序屏幕

我正在使用 Jetpack 导航架构组件。

当前问题和我尝试了什么?

单击第二个和第三个底部导航项会在工具栏上显示后退箭头吗?

尝试过:将与第二和第三底部导航相关的片段设置为顶级目的地

appBarConfig = AppBarConfiguration(setOf(R.layout.fragment_star, R.layout.fragment_stats, R.layout.fragment_user))

代替

appBarConfig = AppBarConfiguration(navController.graph, drawerLayout)

没有工作。

任何帮助高度赞赏!


我的代码如下所示。

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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/drawerLayout"
        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">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

            <fragment
                android:id="@+id/navHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                app:defaultNavHost="true"
                app:navGraph="@navigation/nav_graph" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNav"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="?android:attr/windowBackground"
                app:menu="@menu/menu_bottom" />

        </LinearLayout>

        <!-- gives navDrawer material look-->
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/nav_drawer_menu"
            app:headerLayout="@layout/nav_header"
            android:fitsSystemWindows="true"
            />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

menu_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/starFragment"
        android:icon="@drawable/ic_star_green_48dp"
        android:title="@string/bottom_nav_title_star"/>

    <item
        android:id="@+id/statsFragment"
        android:icon="@drawable/ic_stats_green_48dp"
        android:title="@string/bottom_nav_title_stats"/>

    <item
        android:id="@+id/userFragment"
        android:icon="@drawable/ic_user_green_48dp"
        android:title="@string/bottom_nav_title_user"/>

</menu>

导航图.xml

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

    <fragment
        android:id="@+id/starFragment"
        android:name="com.example.app.ui.StarrFragment"
        android:label="Star"
        tools:layout="@layout/fragment_star">
    </fragment>
    <fragment
        android:id="@+id/statsFragment"
        android:name="com.example.app.StatsFragment"
        android:label="fragment_stats"
        tools:layout="@layout/fragment_stats" />
    <fragment
        android:id="@+id/userFragment"
        android:name="com.example.app.UserFragment"
        android:label="fragment_user"
        tools:layout="@layout/fragment_user" />
</navigation>

ActivityMain.kt

class MainActivity : AppCompatActivity() {

    private lateinit var drawerLayout: DrawerLayout
    private lateinit var appBarConfig: AppBarConfiguration
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        setSupportActionBar(toolbar)

        drawerLayout = binding.drawerLayout
        navController = this.findNavController(R.id.navHostFragment)

        binding.bottomNav.setupWithNavController(navController)

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        appBarConfig = AppBarConfiguration(navController.graph, drawerLayout)


        // lock drawer when not in start destination
        navController.addOnDestinationChangedListener { nc, nd, _ ->

            if(nd.id == nc.graph.startDestination){
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            }
            else{
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            }
        }

         NavigationUI.setupWithNavController(binding.navView, navController)
    }

    override fun onSupportNavigateUp(): Boolean {

        // replace navigation up button with nav drawer button when on start destination
        return NavigationUI.navigateUp(navController, appBarConfig)

    }
}

无需编写单独的代码来用抽屉图标替换后退按钮。

在 AppBarConfiguration 中传递片段 ID(来自 nav_graph),您使用它从底部导航和导航抽屉导航。 PS片段及其相关图标应具有相同的 ID

对于您的情况,AppBarConfiguration 应如下所示:

appBarConfig = AppBarConfiguration.Builder(R.id.starFragment, R.id.statsFragment, R.id.userFragment)
                .setDrawerLayout(drawerLayout)
                .build()

设置操作栏:

setSupportActionBar(toolbar)
setupActionBarWithNavController(navController, appBarConfig)

现在为底部导航和导航视图设置导航控制器:

navView.setupWithNavController(navController)
bottomNav.setupWithNavController(navController)

onSupportNavigateUp 函数应该是:

override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfig)
    }

如果抽屉打开,则应处理后退按钮按下:

override fun onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

奖金

默认情况下,当您依次单击 icon_1、icon_2 和 icon_3 顺序中的底部导航图标时,然后按后退按钮,它将导航回到图标_1 的主页图标

如果您想以与单击图标相反的顺序(返回堆栈方式)向后导航,则将android:menuCategory="secondary" 添加到菜单中的项目。 所以你的菜单会是这样的:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/starFragment"
        android:icon="@drawable/ic_star_green_48dp"
        android:title="@string/bottom_nav_title_star"
        android:menuCategory="secondary"/>
    <item
        android:id="@+id/statsFragment"
        android:icon="@drawable/ic_stats_green_48dp"
        android:title="@string/bottom_nav_title_stats"
        android:menuCategory="secondary"/>
    <item
        android:id="@+id/userFragment"
        android:icon="@drawable/ic_user_green_48dp"
        android:title="@string/bottom_nav_title_user"
        android:menuCategory="secondary"/>
</menu>

希望现在可以解决后退按钮图标:)

这个项目使用DrawerLayout并使用RadioButtons模拟BottomNavigationView,这是我找到的解决问题的方法


在 Google Codelab 导航项目中,他们执行了 Adithya T Raj 提到的操作,但它仅用于在横向上显示 DrawerLayout 和在纵向上显示 BottomNavigationView。 项目链接

我试图强迫他们在这个分支上向我展示两者但只显示了 DrawerLayout

我有如下屏幕,其中在同一屏幕上包含一个导航抽屉和底部导航:

应用程序屏幕

我正在使用Jetpack导航体系结构组件。

当前问题和我尝试了什么?

单击第二个和第三个底部导航项在工具栏上显示后退箭头?

尝试过:将与第二个和第三个底部导航相关的片段设置为顶级目的地

appBarConfig = AppBarConfiguration(setOf(R.layout.fragment_star, R.layout.fragment_stats, R.layout.fragment_user))

代替

appBarConfig = AppBarConfiguration(navController.graph, drawerLayout)

没有工作。

任何帮助,高度赞赏!


我的代码如下所示。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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/drawerLayout"
        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">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

            <fragment
                android:id="@+id/navHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                app:defaultNavHost="true"
                app:navGraph="@navigation/nav_graph" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNav"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="?android:attr/windowBackground"
                app:menu="@menu/menu_bottom" />

        </LinearLayout>

        <!-- gives navDrawer material look-->
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/nav_drawer_menu"
            app:headerLayout="@layout/nav_header"
            android:fitsSystemWindows="true"
            />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

menu_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/starFragment"
        android:icon="@drawable/ic_star_green_48dp"
        android:title="@string/bottom_nav_title_star"/>

    <item
        android:id="@+id/statsFragment"
        android:icon="@drawable/ic_stats_green_48dp"
        android:title="@string/bottom_nav_title_stats"/>

    <item
        android:id="@+id/userFragment"
        android:icon="@drawable/ic_user_green_48dp"
        android:title="@string/bottom_nav_title_user"/>

</menu>

nav_graph.xml

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

    <fragment
        android:id="@+id/starFragment"
        android:name="com.example.app.ui.StarrFragment"
        android:label="Star"
        tools:layout="@layout/fragment_star">
    </fragment>
    <fragment
        android:id="@+id/statsFragment"
        android:name="com.example.app.StatsFragment"
        android:label="fragment_stats"
        tools:layout="@layout/fragment_stats" />
    <fragment
        android:id="@+id/userFragment"
        android:name="com.example.app.UserFragment"
        android:label="fragment_user"
        tools:layout="@layout/fragment_user" />
</navigation>

ActivityMain.kt

class MainActivity : AppCompatActivity() {

    private lateinit var drawerLayout: DrawerLayout
    private lateinit var appBarConfig: AppBarConfiguration
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        setSupportActionBar(toolbar)

        drawerLayout = binding.drawerLayout
        navController = this.findNavController(R.id.navHostFragment)

        binding.bottomNav.setupWithNavController(navController)

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        appBarConfig = AppBarConfiguration(navController.graph, drawerLayout)


        // lock drawer when not in start destination
        navController.addOnDestinationChangedListener { nc, nd, _ ->

            if(nd.id == nc.graph.startDestination){
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            }
            else{
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            }
        }

         NavigationUI.setupWithNavController(binding.navView, navController)
    }

    override fun onSupportNavigateUp(): Boolean {

        // replace navigation up button with nav drawer button when on start destination
        return NavigationUI.navigateUp(navController, appBarConfig)

    }
}

在Google Codelab导航项目中,它们执行了Adithya T Raj提到的操作,但仅用于在横向上显示DrawerLayout,在纵向上显示BottomNavigationView。 项目链接: https : //github.com/googlecodelabs/android-navigation

我试图强迫他们在该分支上同时显示我但只显示DrawerLayout

首先,这不是一个好的设计方法。 如果你想要类似的东西,你应该使用底部应用栏(而不是带有导航抽屉的底部导航视图)

这是您的指南: 文章链接

您可以轻松地调整您的应用程序。

暂无
暂无

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

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