android – 导航组件,控制何时显示汉堡包或后退图标

前端之家收集整理的这篇文章主要介绍了android – 导航组件,控制何时显示汉堡包或后退图标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下活动
class MainActivity : AppCompatActivity() {

private lateinit var drawerLayout: androidx.drawerlayout.widget.DrawerLayout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)

    drawerLayout = drawer_layout

    val navController = Navigation.findNavController(this,R.id.fragment_main_navHost)

    setSupportActionBar(toolbar)

    NavigationUI.setupActionBarWithNavController(this,navController,drawerLayout)
    navView_main.setupWithNavController(navController)
}

override fun onSupportNavigateUp(): Boolean {
    return NavigationUI.navigateUp(drawerLayout,Navigation.findNavController(this,R.id.fragment_main_navHost))
}

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

你可以看到它与导航图相关联,我正在使用导航抽屉.当我浏览抽屉中的项目时,我想保留汉堡图标,只有当我点击片段或弹出窗口中的项目时才将其更改为上/后按钮,并确保系统的行为反映了什么用户期望基于显示的图标.那可能吗

解决方法

按照以下步骤操作

1.绑定你的NavigationView with NavigationUI

NavigationUI.setupWithNavController(nav_view,hostFragment.navController)

2.绑定ActionBar With NavController

NavigationUI.setupActionBarWithNavController(this@NavActivity,hostFragment.navController)

3.绑定ActionBar and DrawerLayout With NavController

NavigationUI.setupActionBarWithNavController(this@NavActivity,hostFragment.navController,drawer_layout)

4.覆盖您活动中的onSupportNavigateUp()

override fun onSupportNavigateUp(): Boolean {
    return NavigationUI.navigateUp(drawer_layout,hostFragment.navController) 
              || super.onSupportNavigateUp()
}

样品:

class NavActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {

    lateinit var hostFragment: NavHostFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_nav)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG)
                    .setAction("Action",null).show()
        }

        val toggle = ActionBarDrawerToggle(
                this,drawer_layout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)

        hostFragment = supportFragmentManager.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment    
        NavigationUI.setupWithNavController(nav_view,hostFragment.navController)    
        NavigationUI.setupActionBarWithNavController(this@NavActivity,drawer_layout)    
    }

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

    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(drawer_layout,hostFragment.navController) || super.onSupportNavigateUp()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.nav,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button,so long
        // as you specify a parent activity in AndroidManifest.xml.
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }
}

产量

家庭片段:

片段二:

片段树:

原文链接:/android/315173.html

猜你在找的Android相关文章