1

我正在使用 androidx.navigation.ui 实现导航抽屉。

基于Android Studio的Navigation Drawer Activity Template,当我放入根据抽屉动画工作的onDrawerSlide时,AppBarConfigration中设置的topLevelDesitnationIds被忽略,导航按钮(不是抽屉图标,而是返回按钮)会显示在非顶级片段。即使我按下它,抽屉每次都会出来。

val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_content_main)

//-- START : It will come out a drawer menu regardless of the fragment stack

class LocalActionBarDrawerToggle : ActionBarDrawerToggle(this,
    drawerLayout,
    binding.appBarMain.toolbar,
    R.string.navigation_drawer_open,
    R.string.navigation_drawer_close) {
    override fun onDrawerSlide(drawerView: View, slideOffset: Float) {

        // do something

        super.onDrawerSlide(drawerView, slideOffset)
    }
}
val drawerToggle = LocalActionBarDrawerToggle()
drawerLayout.addDrawerListener(drawerToggle)
drawerToggle.syncState()
//-- END

appBarConfiguration = AppBarConfiguration(
    setOf(
        R.id.nav_home
    ), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)

对于非顶级片段,导航按钮是后退按钮而不是抽屉图标,所以我认为它只是覆盖了运动控制。请给我一些建议。

4

0 回答 0