android - Android - 在导航控件中,如何关闭导航DrawerLayout onBackPressed

我已经创建了一个带有导航模式的Androidx项目,现在我想在fragment中关闭drawerLayout onBackPressed() ,我还想把OnClickListener设置到自定义菜单中,不可能重写fragment中的onBackPressed()

我想知道如何才能做到这一点?

这是我的drawer :


<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中实现?

时间:

你可以在Fragment中实现,方法是在你的drawer Activity上实现OnBackStackChangedListener ,
然后,用ActionBarDrawerToggle链接你的drawer并覆盖onBackStackChanged()并同步你的drawer切换。

以下是完整的代码段:


public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener {



 private DrawerLayout drawer;


 private ActionBarDrawerToggle toggle;



 @Override


 protected void onCreate(Bundle savedInstanceState) {


 super.onCreate(savedInstanceState);


 //Bind listener here


 getSupportFragmentManager().addOnBackStackChangedListener(this);


 drawer = findViewById(R.id.drawer_layout);


 toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);


 toggle.setToolbarNavigationClickListener(view -> onBackPressed());


 drawer.addDrawerListener(toggle);


 toggle.syncState();



 }



 //Override fragment backstack lister method 


 @Override


 public void onBackStackChanged() {


 //toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);


 //getSupportActionBar().setDisplayHomeAsUpEnabled(getSupportFragmentManager().getBackStackEntryCount() > 0);


 toggle.syncState();


 }



 @Override


 public void onBackPressed() {


 drawer = findViewById(R.id.drawer_layout);


 if (drawer.isDrawerOpen(GravityCompat.START))


 drawer.closeDrawer(GravityCompat.START);


 else {


 //super.onBackPressed();


 }


 }


}



我希望这对你有帮助。

简短回答

不能在Fragment内拦截onBackPressed回调,而且在宿主Activity中添加额外的代码是不可能的,它在Android SDK中没有实现,停止。

原始答案

Fragment类中没有onBackPressed回调,但你可以自己实现它:

创建一个接口:


interface FragmentOnBackPressedListener {


 fun onBackPressed()


}



现在在你的Activity中:


override fun onBackPressed() {


 // find top fragment


 val currentFragment = supportFragmentManager


 .findFragmentById(R.id.your_nav_host_fragment_id)?.let {


 it.childFragmentManager.fragments.getOrNull(0)


 }


 // check if it implements FragmentOnBackPressedListener 


 if (currentFragment is FragmentOnBackPressedListener ) {


 // if it does, transfer flow to the fragment


 return currentFragment.onBackPressed()


 }


 // if it doesn't, apply default behaviour


 super.onBackPressed()


}



然后在你的fragment中:


class ExampleFragment : Fragment(), FragmentOnBackPressedListener {



 override fun onBackPressed() {


 // close your drawer layout here if visible



 // Also: to close fragment, invoke 


 // findNavController().navigateUp() or findNavController().popBackStack()


 }



}



你好,你可以通过在Activity中获取fragment,并且使用kotlin代码覆盖onBackPress来完成,

这是你的backpress函数,在这里你可以获得你的导航,然后你可以检查你的当前fragment 。


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


 }


}



下面是选择项监听器


override fun onNavigationItemSelected(item: MenuItem): Boolean {


 drawerLayoutHome.closeDrawer(GravityCompat.START)



 return true


}



在Kotlin中,只需在Activity中重写此方法,


override fun onBackPressed() {


 if (drawer_layout.isDrawerOpen(GravityCompat.START))


 drawer_layout.closeDrawer(GravityCompat.START)


 else


 super.onBackPressed()


}



...