I have created an Androidx project with Navigation pattern. Now I want to close drawerLayout onBackPressed() in fragment. And also I want to set OnClickListener to drawer menu items to my custom menu there is no way I guess to override the onBackPressed() in fragment.

I want to know how I can achieve this?

这是我的抽屉:

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/drawerLayoutHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:menu="@menu/activity_main_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>

我想做到这一点:

override fun onBackPressed() {

    if (drawerLayoutHome.isDrawerOpen(GravityCompat.END)) {
        drawerLayoutHome.closeDrawer(GravityCompat.END)
    } else {
        super.onBackPressed()
    }
}

但这是Activity年,如何在Fragment年实现这一目标?

推荐答案

Hello there you can do it by getting your fragment in activity and override onbackpress with kotlin code

This is your backpress function and in this you can get your nav host and then you can check your current fragment or the fragment you want to open drawer in .

override fun onBackPressed() {

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentHomeHost)
    val completeFragmentList = navHostFragment?.childFragmentManager?.fragments

    if (completeFragmentList != null) {
        when {
            completeFragmentList[0] is HomeFragment -> {
                val home = (completeFragmentList[0] as HomeFragment)


            }
            else -> super.onBackPressed()
        }
    }

    if (drawerLayoutHome.isDrawerOpen(GravityCompat.START)) {
        drawerLayoutHome.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

这是你可以得到的 Select 项侦听器

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    drawerLayoutHome.closeDrawer(GravityCompat.START)

    return true
}

Kotlin相关问答推荐

Kotlin -从列表中获取特定过滤器的唯一列表值

使用另一个对象的列表创建对象

从 Kotlin 的父类获取函数注解

kotlin中如何使用子类调用父静态方法?

Spring Boot Kotlin 数据类未使用 REST 控制器中的默认值初始化

Kotlin 如何使用其 get 函数在内部检索映射值

如何在 Spring Boot 3 中为内部类提供运行时提示

Kotlin 日期格式从一种更改为另一种

类型是什么意

如何使 TextInputEditText 只读?

无法在 kotlin android 中以编程方式禁用 EditText

Kotlin 类的调用方法

kotlin:扩展方法和空接收器

使用 clear() 删除 EncryptedSharedPreferences 不起作用

Kotlin 具体化的泛型不会按计划保持类型

在Kotlin中传递并使用函数作为构造函数参数

RecyclerView SnapHelper无法显示第一个/最后一个元素

Kotlin中保留的关键字是什么?

如何在Kotlin中使方法param可变?

Java的Kotlin:字段是否可以为空?