【问题标题】:How stop click on same bottom navigation item?如何停止单击相同的底部导航项?
【发布时间】:2019-10-30 16:04:31
【问题描述】:

我有一个带有 4 个菜单项的底部导航。

如果用户在主页导航片段中并且他再次单击主页导航项,则该片段正在重新创建。

如何禁用当前导航菜单项的点击?

这是我的导航代码:

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_feed, R.id.navigation_profile, R.id.trips_feed).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

【问题讨论】:

  • 你找到解决办法了吗?

标签: java android android-fragments


【解决方案1】:

您需要使用 switch case 来管理它 所以得到现在打开的片段 然后说如果片段打开了,就再也不会打开了

示例代码:

片段片段 = null;

    List<Fragment> fragments = getSupportFragmentManager().getFragments();

    for (Fragment currentFragment : fragments) {


        switch (item.getItemId()) {

            case R.id.navigation_home:
                if (!(currentFragment instanceof HomeFragmentGeneral)) {
                    fragment = HomeFragmentGeneral.newInstance();
                }
                break;


            case R.id.navigation_search:
                if (!(currentFragment instanceof NearlyFrag)) {
                    fragment = NearlyFrag.newInstance();
                }
                break;

            case R.id.navigation_profile:
                if (!(currentFragment instanceof ProfileFragment)) {
                    fragment = ProfileFragment.newInstance();
                }
                break;
        }
    }


    if (fragment != null) {
        attachFragmentToActivity(fragment, R.id.frame);
    }

【讨论】:

  • 嗨 Genius8020,我对 android 很陌生,我如何添加 onclick 监听器,以获取项目,在上面提到的解决方案中 item.getItemId() 我如何得到它。
  • 你必须在onNavigationItemSelected方法输入里面添加item menu...请给我投票
猜你喜欢
  • 1970-01-01
  • 2018-04-25
  • 2020-02-11
  • 1970-01-01
  • 2021-11-27
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
相关资源
最近更新 更多