简体   繁体   中英

Android navigation component side navigation just on specific fragment

How can I use side navigation (DrawerLayout + NavigationView) just on main_fragment and profile_fragment ? login_fragment should be without DrawerLayout and NavigationView, just Toolbar and content.

Navigation graph:

You should create the drawer in your activity and than handle it's visibility in addOnDestinationChangedListener :

 navController.addOnDestinationChangedListener { _, destination, _ ->
            if (destination.id == R.id.someFragment) {
                toolbar.visibility = View.GONE
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            } else {
                toolbar.visibility = View.VISIBLE
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            }
        }

Don't do this in fragments, it's not a good pattern not that it doesn't compile or something.

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