美文网首页安卓开发安卓开发Android开发经验谈
安卓实现侧滑菜单加底部导航栏:DrawerLayout+Bott

安卓实现侧滑菜单加底部导航栏:DrawerLayout+Bott

作者: 蓝不蓝编程 | 来源:发表于2019-03-27 20:07 被阅读1次

    效果图

    image

    主要代码

    1. MainActivity
    package com.cxyzy.myapplication
    
    import android.os.Bundle
    import android.view.Menu
    import android.view.MenuItem
    import androidx.appcompat.app.ActionBarDrawerToggle
    import androidx.appcompat.app.AppCompatActivity
    import androidx.core.view.GravityCompat
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.Observer
    import androidx.navigation.NavController
    import androidx.navigation.ui.setupActionBarWithNavController
    import com.google.android.material.bottomnavigation.BottomNavigationView
    import com.google.android.material.navigation.NavigationView
    import kotlinx.android.synthetic.main.activity_main.*
    import kotlinx.android.synthetic.main.app_bar_main.*
    
    class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    
        private var currentNavController: LiveData<NavController>? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            if (savedInstanceState == null) {
                setupBottomNavigationBar()
            }
            setSupportActionBar(toolbar)
    
            val toggle = ActionBarDrawerToggle(
                    this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
            )
            drawerLayout.addDrawerListener(toggle)
            toggle.syncState()
    
            naviView.setNavigationItemSelectedListener(this)
        }
    
        override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
            super.onRestoreInstanceState(savedInstanceState)
            setupBottomNavigationBar()
        }
    
        private fun setupBottomNavigationBar() {
            val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
    
            val navGraphIds = listOf(R.navigation.navi_first, R.navigation.navi_second)
    
            // Setup the bottom navigation view with a list of navigation graphs
            val controller = bottomNavigationView.setupWithNavController(
                    navGraphIds = navGraphIds,
                    fragmentManager = supportFragmentManager,
                    containerId = R.id.nav_host_container,
                    intent = intent
            )
    
            // Whenever the selected controller changes, setup the action bar.
            controller.observe(this, Observer { navController ->
                setupActionBarWithNavController(navController)
            })
            currentNavController = controller
        }
    
        override fun onSupportNavigateUp(): Boolean {
            return currentNavController?.value?.navigateUp() ?: false
        }
    
        /**
         * Overriding popBackStack is necessary in this case if the app is started from the deep link.
         */
        override fun onBackPressed() {
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawer(GravityCompat.START)
            }
            if (currentNavController?.value?.popBackStack() != true) {
                super.onBackPressed()
            }
        }
    
        override fun onCreateOptionsMenu(menu: Menu): Boolean {
            menuInflater.inflate(R.menu.menu_toolbar, menu)
            return true
        }
    
        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            return when (item.itemId) {
                R.id.action_settings -> true
                else -> super.onOptionsItemSelected(item)
            }
        }
    
        override fun onNavigationItemSelected(item: MenuItem): Boolean {
            when (item.itemId) {
                R.id.nav_camera -> {
                }
                R.id.nav_gallery -> {
                }
                R.id.nav_share -> {
                }
                R.id.nav_send -> {
                }
            }
    
            drawerLayout.closeDrawer(GravityCompat.START)
            return true
        }
    }
    
    1. activity_main.xml
     <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.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/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="end">
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/naviView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    Demo源代码

    https://gitee.com/cxyzy1/navigationDemo/tree/master/withDrawerLayout

    安卓开发技术分享: https://www.jianshu.com/p/442339952f26
    更多技术总结好文,请关注:「程序园中猿」

    相关文章

      网友评论

        本文标题:安卓实现侧滑菜单加底部导航栏:DrawerLayout+Bott

        本文链接:https://www.haomeiwen.com/subject/piitbqtx.html