How to set up Navigation Component with Navigation Drawer in Android?

前端 未结 1 1828
暗喜
暗喜 2021-02-05 20:11

How do I set up navigation component with navigation drawer? How do I use it in my app?

Can everything be done with one Activity?

How do I handle toolbar visibi

1条回答
  •  死守一世寂寞
    2021-02-05 20:56

    How do I set up navigation component with navigation drawer?

    How do I use it in my app?

    The navigation drawer set up differs a little when it comes to Navigation component.

    Note, if you create a new app with drawer navigation, the current tutorial is not needed. However I'm going to explain some things that might look strange around here and if you decide to add a drawer at a later stage of the app

    First, you need to set up your activity_main.xml and MainActivity to be ready for the Navigation Architecture:

    
    
    
        
    
        
    
    
    

    where app_bar_main is just:

    
    
    
        
    
            
    
        
    
        
    
    
    

    And the content_main is where your fragments are going to be held:

    
    
    
        
    
    

    Things you should know: The activity mustn't have a AppBar set on AndroidManifest.xml:

    android:theme="@style/AppTheme.NoActionBar"
    

    If you notice app:menu="@menu/activity_main_drawer" in the NavigationView tag, the fragment names should be the same that they are inside the mobile_navigation.xml:

    
            
            
            
            
        
    
        
            
                
                
            
        
    
    
    

    This way, with what is going to be explained below there will be no need to call for onCreateOptionsMenu to detect the clicks. Android team has already solved that for us. Follow below.

    Until now, this doesn't differ too much from the usual drawer set up we actually do. But, there are some configurations we would need to do in the logic part of the app. So let's open MainActivity.kt. First you will need these:

      private var appBarConfiguration: AppBarConfiguration? = null
      private var drawerLayout: DrawerLayout? = null
      private var toolbar: Toolbar? = null
      private var navController: NavController? = null
    

    After that in your onCreate method:

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            toolbar = findViewById(R.id.toolbar)
            setSupportActionBar(toolbar) //set the toolbar
    
            drawerLayout = findViewById(R.id.drawer_layout)
            val navView: NavigationView = findViewById(R.id.nav_view)
            navController = findNavController(R.id.nav_host_fragment)
            appBarConfiguration = AppBarConfiguration(
                setOf(
                    R.id.homeFragment,
                    R.id.galleryFragment,
                    R.id.slideShowFragment,
                    R.id.toolsFragment,
                    R.id.shareFragment,
                    R.id.sendFragment,
                    R.id.loginFragment,
                    R.id.phoneConfirmationFragment
                ), drawerLayout
            )
    
            setupActionBarWithNavController(navController!!, appBarConfiguration!!) //the most important part
            navView.setupWithNavController(navController!!) //the second most important part
    
          //other things unrelated
        }
    

    Let's see what's going on here:

    First you would need a reference to the navController. The AppBarConfiguration is just a class which holds the fragments that are going to be opened as top level destinations. Which means that the fragment which is going to be opened after them would release the current one from the fragment back stack. It's important to tell to the AppBarConfiguration that we have a drawer also (passed as a parameter in the constructor).

    Down below you would have a method called onSupportNavigateUp():

    override fun onSupportNavigateUp(): Boolean {
            val navController = findNavController(R.id.nav_host_fragment)
            return navController.navigateUp(appBarConfiguration!!) || super.onSupportNavigateUp()
        }
    

    This method has to do with the up back button. But you won't need it too much if you have drawer navigation. This really comes in handy when you have a lot of fragments added on the backstack (or at least two).

    Can everything be done with one Activity?

    Yes, definitely! but still it requires a little bit more work when it comes to conditional navigation. Like when you want to show fragments which are not part of your drawer app. But still Google has done a huge progress with it. You can refer to conditional navigation here.

    How do I handle toolbar visibility with just one Activity and fragments which have a dynamic toolbar visibility. Also, there are fragments which I need to close the drawer and make it inaccessible.

    You can use addOnDestinationChangedListener from the navController:

    navController.addOnDestinationChangedListener { _, destination, _ ->
                when (destination.id) {
                    R.id.loginFragment, R.id.registerFragment, R.id.phoneConfirmationFragment -> {
                        toolbar?.visibility = View.GONE
                        drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                    }
                    else -> {
                        toolbar?.visibility = View.VISIBLE
                        drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
                    }
                }
            }
    

    Now you have a drawer and navigation component on your app.

    0 讨论(0)
提交回复
热议问题