gpt4 book ai didi

java - 如何一起使用 AppBarConfiguration 和 setNavigationItemSelectedListener

转载 作者:行者123 更新时间:2023-12-05 00:13:41 27 4
gpt4 key购买 nike

我的导航菜单中有 8 个项目。我为其中的 5 个设置了 fragment ,我想为最后 3 个设置 toast 。
当我不设置案例 2 时,案例 1 运行良好。但是当我设置案例 2 时,案例 1 不起作用。

案例一:

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.

//define in mobile_navigation.xml
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_update_data, R.id.nav_notifications,
R.id.nav_stop, R.id.nav_change_pass)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

案例2:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.nav_exit:
{
Toast.makeText(Main2Activity.this,"gbfg",Toast.LENGTH_SHORT).show();
break;
}
case R.id.nav_get_update_app:
{
break;
}
case R.id.nav_about_us:
{
break;
}
}
return false;
}
});

最佳答案

您必须使用 NavigationUI.onNavDestinationSelected 处理此问题打回来。检查以下:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

boolean handled = NavigationUI.onNavDestinationSelected(item, navController);

if (!handled) {
switch (item.getItemId()) {
case R.id.nav_exit: {
Toast.makeText(Main2Activity.this, "gbfg", Toast.LENGTH_SHORT).show();
break;
}
case R.id.nav_get_update_app: {
break;
}
case R.id.nav_about_us: {
break;
}
}
}

drawer.closeDrawer(GravityCompat.START);
return handled;
}
});

旁边的 , 您必须调用 NavigationItemSelectedListener调用 setupWithNavController 后如下所示:
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(...);

关于java - 如何一起使用 AppBarConfiguration 和 setNavigationItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60147039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com