按下导航菜单项将不响应

按下导航菜单项将不响应,第1张

按下导航菜单项将不响应

线

NavigationUI.setupWithNavController(navigationView, navController);

setNavigationItemSelectedListener
内部调用以将目标连接到菜单项(即,当您单击
R.id.nav_settings
MenuItem时,它将用已
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;}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5020670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存