Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何弥补按钮返回而不是打开导航抽屉_Android_Kotlin_Navigation_Androidx - Fatal编程技术网

Android 如何弥补按钮返回而不是打开导航抽屉

Android 如何弥补按钮返回而不是打开导航抽屉,android,kotlin,navigation,androidx,Android,Kotlin,Navigation,Androidx,我成功地在我的应用程序中实现了一个导航抽屉,将菜单的每个项目链接到一个片段。目标片段隐藏抽屉切换并显示向上按钮(即箭头图标),但无论出于何种原因,如果我单击它,它将打开抽屉,而我无法返回到上一个片段。我必须按后退按钮才能做这件事。我怎样才能改变这种行为?不在每个片段中添加代码就可以解决这个问题吗 我使用的是: 导航组件 一个活动,一些片段 工具栏 AppBarConfiguration main活动 class MainActivity : AppCompatActivity() {

我成功地在我的应用程序中实现了一个导航抽屉,将菜单的每个项目链接到一个片段。目标片段隐藏抽屉切换并显示向上按钮(即箭头图标),但无论出于何种原因,如果我单击它,它将打开抽屉,而我无法返回到上一个片段。我必须按后退按钮才能做这件事。我怎样才能改变这种行为?不在每个片段中添加代码就可以解决这个问题吗

我使用的是:

  • 导航组件
  • 一个活动,一些片段
  • 工具栏
  • AppBarConfiguration
main活动

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Setup FTUE conditional navigation
        setupNavigation()

        setupNavigationDrawer()

        appBarConfiguration =
            AppBarConfiguration(
                setOf(R.id.overviewFragment, R.id.registrationFragment,
                       R.id.registrationParamFragment), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        findViewById<NavigationView>(R.id.nav_view)
            .setupWithNavController(navController)
    }

    /**
     * Set the toolbar, set the drawerLayout, set the drawerToggle
     */
    private fun setupNavigationDrawer() {
        toolbar = findViewById(R.id.toolbar)

        setSupportActionBar(toolbar)

        drawerLayout = (findViewById<DrawerLayout>(R.id.drawer_layout))
            .apply {
                setStatusBarBackground(R.color.colorPrimaryDark)
            }
        drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
        drawerToggle.setDrawerIndicatorEnabled(true)
        drawerToggle.syncState()

        drawerLayout.addDrawerListener(drawerToggle)

    }

    override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onPostCreate(savedInstanceState, persistentState)
        drawerToggle.syncState()
    }

    /**
     * Ensures that the drawer is closed
     * before the app switches back to the previous fragment
     */
    override fun onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }
}
class MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//设置FTUE条件导航
设置导航()
setupNavigationDrawer()
appBarConfiguration=
AppBarConfiguration(
集合(R.id.overviewFragment,R.id.registrationFragment,
R.id.registrationParamFragment),抽屉布局)
setupActionBarWithNavController(navController、appBarConfiguration)
findViewById(R.id.nav_视图)
.setupWithNavController(navController)
}
/**
*设置工具栏,设置抽屉布局,设置抽屉切换
*/
私人娱乐设置导航抽屉(){
toolbar=findviewbyd(R.id.toolbar)
设置支持操作栏(工具栏)
抽屉布局=(findViewById(R.id.drawer\U布局))
.申请{
setStatusBarBackground(R.color.colorPrimaryDark)
}
抽屉切换=ActionBarDrawerToggle(这个,抽屉布局,工具栏,R.string.open,R.string.close)
抽屉切换。SetDroperIndicatorEnabled(真)
drawerToggle.syncState()
抽屉布局。添加抽屉链接(抽屉切换)
}
重写PostCreate(savedInstanceState:Bundle?,persistentState:PersistableBundle?){
super.onPostCreate(savedInstanceState、persistentState)
drawerToggle.syncState()
}
/**
*确保抽屉已关闭
*在应用程序切换回上一个片段之前
*/
重写函数onBackPressed(){
if(抽屉布局isDrawerOpen(重力比较开始)){
抽屉布局。关闭抽屉(重力公司启动)
}否则{
super.onBackPressed()
}
}
}

完整存储库:

为什么不使用继承?创建包含此代码的
MonitorCardAcoActivity
,并且您使用的每个活动都应继承MonitorCardAcoActivity。

您需要使用导航控制器设置工具栏:

toolbar.setupWithNavController(findNavController(R.id.nav_host_fragment),findViewById(R.id.full_drawer_layout))

这方面还有更多信息。

回购的链接已断开。您找到答案了吗?