我有以下活动
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()
}
}
您可以看到与导航图相关联,并且我正在使用导航抽屉。当我浏览抽屉中的项目时,我想保留汉堡包图标,并且仅当单击片段或弹出窗口中的项目时才将其更改为上/后按钮,并确保系统的行为反映出用户期望基于显示的图标。可能
答案 0 :(得分:12)
要控制何时向上/向后显示AppBar导航,需要执行以下操作
1-创建AppBarConfiguration并将顶级目的地和抽屉布局传递给它
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.dest_one,
R.id.dest_two
),
drawerLayout
)
2-告诉AppBar有关配置和导航的信息。这将有助于显示标题并显示箭头或抽屉菜单图标
setupActionBarWithNavController(navController, appBarConfig)
3-最后,重写onOptionsItemSelected和onSupportNavigateUp和导航组件扩展,以通知AppBar行为
override fun onOptionsItemSelected(item: MenuItem)= item.onNavDestinationSelected(findNavController(R.id.my_nav_host_fragment))
|| super.onOptionsItemSelected(item)
override fun onSupportNavigateUp() = findNavController(R.id.my_nav_host_fragment).navigateUp(appBarConfiguration)
参考Google Code Lab导航Navigation Codelab
答案 1 :(得分:5)
按照此步骤
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, hostFragment.navController)
NavigationUI.setupActionBarWithNavController(this@NavActivity, hostFragment.navController, 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
}
}
答案 2 :(得分:0)
因此,我认为您可以使用 NavController.OnNavigatedListener 监听将显示的片段,然后更新工具栏图标。
val navController = Navigation.findNavController(this, R.id.fragment_main_navHost)
navController.addOnNavigatedListener(contoller, destination -> {
if(destination.id == R.id.fragmentTwo){
// change the toolbar icon here
}
})
对不起,我这里没有电脑,所以我在没有任何IDE的情况下编写了此代码,这可能会出错。但是,请接受这个想法。
希望这对您有所帮助。