简体   繁体   中英

how to add both bottom navigation and navigation drawer in an android app

Currently I have three options in my bottom navigation and a navigation graph for them.

part of my mainActivity.xml file look like this:

<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/nav_graph"
        app:defaultNavHost = "true" />

    <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" />

and In my mainActivity I have written this code,

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

everything related to navigation is handled by jetpack navigation library. Now I want to add a Navigation drawer also and in drawer I want to add different menu items(not only three that are in bottom navigation), so I will add new menu resource file for navigation drawer, now how should I use navigation library for both bottom nav and nav drawer? I don't want to manually do Fragment transactions and work with fragment manager.

One approach that I can think of is adding all of the fragments in a single nav graph(which is currently used for bottom nav) and then use the same navController for nav drawer also but I am looking for a better approach.

The structure of drawer is you have to include activity INSIDE drawer layout. So in your case the implementation of the bottom navigation is you include bottom_navigation INSIDE main_activity ---> main_activity INSIDE drawer_layout

Code sample

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
        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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

    <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

SRC https://code.tutsplus.com/tutorials/how-to-code-a-navigation-drawer-in-an-android-app--cms-30263

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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