31

我有以下活动

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()
    }
}

如您所见,它与导航图相关联,我正在使用导航抽屉。当我浏览抽屉中的项目时,我想保留汉堡图标,并且仅在单击片段或弹出窗口中的项目时将其更改为上/后退按钮,并确保系统的行为反映了什么用户期望基于显示的图标。那可能吗

4

3 回答 3

27

要控制 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)

参考谷歌代码实验室导航导航Codelab

于 2018-12-18T16:14:14.467 回答
10

按照这个步骤

1.绑定你NavigationViewNavigationUI

NavigationUI.setupWithNavController(nav_view, hostFragment.navController)

2.绑定 ActionBar_NavController

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

3.绑定ActionBarDrawerLayout使用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
    }
}

输出

家庭片段:

家庭片段

片段二:

片段二

片段树:

片段树

于 2018-09-11T06:39:15.803 回答
2

因此,我认为您可以使用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 的情况下编写此代码,这可能会出错。但是接受这个想法。

希望这对您有所帮助。

于 2018-09-11T17:28:31.097 回答