线
NavigationUI.setupWithNavController(navigationView, navController);
在
setNavigationItemSelectedListener内部调用以将目标连接到菜单项(即,当您单击
R.id.nav_settingsMenuItem时,它将用已
android:id="@+id/nav_settings"设置的菜单项替换NavHostFragment中的Fragment
)。该侦听器将覆盖
OnNavigationItemSelectedListener您设置的视图,这就是为什么您的自定义逻辑不会运行的原因。
如果要将两组功能组合在一起,则需要在
navigationView.setNavigationItemSelectedListener(this);
之后
调用
setupWithNavController并使用触发默认行为
NavigationUI.onNavDestinationSelected():
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_driver_home); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = findViewById(R.id.drawer_layout); NavigationView navigationView = findViewById(R.id.nav_view); // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. mAppBarConfiguration = new AppBarConfiguration.Builder( R.id.nav_home, R.id.nav_history, R.id.nav_settings, R.id.nav_help, R.id.nav_signout) .setDrawerLayout(drawer) .build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration); NavigationUI.setupWithNavController(navigationView, navController); // This line needs to be after setupWithNavController() navigationView.setNavigationItemSelectedListener(this);}@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); switch (menuItem.getItemId()){ case R.id.nav_history: Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show(); break; case R.id.nav_signout: signOut(); break; default: // Trigger the default action of replacing the current // screen with the one matching the MenuItem's ID NavigationUI.onNavDestinationSelected(menuItem, navController); } DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)