且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android导航抽屉单击事件问题

更新时间:2023-01-10 10:34:17

调用 NavigationUI.setupWithNavController(navigationView,navController)时,表示您要 NavController 处理来自NavigationView的点击事件,并根据

When you call NavigationUI.setupWithNavController(navigationView, navController), you're saying that you want NavController to handle click events from your NavigationView, navigating to the related screen as per the NavigationUI documentation. This, by necessity, calls setNavigationItemSelectedListener() internally.

此后,通过调用 setNavigationItemSelectedListener ,您将删除原始侦听器,这就是为什么您的其他项不再执行任何操作的原因.您可以通过调用

By calling setNavigationItemSelectedListener afterwards, you remove the original listener, which is why your other items don't do anything anymore. You can trigger the default behavior by calling NavigationUI.onNavDestinationSelected()

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

    int id = menuItem.getItemId();
    if (id == R.id.callUs) {
        Intent intent = new Intent(Intent.ACTION_CALL);

        intent.setData(Uri.parse("tel:" + "XXXxxxXXX"));

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
        }
        else
        {
            startActivity(intent);
        }
   }
   else
   {
       // Make your navController object final above
       // or call Navigation.findNavController() again here
       NavigationUI.onNavDestinationSelected(menuItem, navController);
   }
   drawer.closeDrawer(GravityCompat.START);
   return true;

}