当所选菜单项相同时,不会调用 onNavigationItemSelected

发布于 2025-01-11 02:36:06 字数 473 浏览 0 评论 0 原文

我有一个简单的 BottomNavigationView ,其中有两个菜单项(主页片段、设置片段)。

我已经实现了 onNavigationItemSelectedListener 和 onNavigationItemSelected。

还有bottomNavigationView.setOnNavigationItemSelectedListener(this);

应用程序页面登陆主页片段。

当我在菜单项之间切换时,会调用 onNavigationItemSelected ,但是当我第一次启动应用程序并点击同一菜单项(即主页片段)时,不会调用 onNavigationItemSelected

当用户已经位于主页但未触发 onNavigationItemSelected 事件时,我需要在用户单击主页时显示 toast。

I have a simple BottomNavigationView with two menu items (Home Fragment, Settings Fragment) in an activity.

I have implemented onNavigationItemSelectedListener and onNavigationItemSelected.

Also bottomNavigationView.setOnNavigationItemSelectedListener(this);

App page lands on the Home Fragment.

onNavigationItemSelected is being called when I switch between menu items but When I first launch the app and tap on the same menu Item i.e. Home Fragment, onNavigationItemSelected is not being called.

I would need to show a toast whenever the user clicks on the home page when user is already in home page but onNavigationItemSelected event is not triggered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2025-01-18 02:36:06

正如迈克 M 提到的,

setOnNavigationItemReselectedListener 做到了这一点。

As Mike M mentioned,

setOnNavigationItemReselectedListener did the trick.

随心而道 2025-01-18 02:36:06

首先,我们在 MainActivity

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int v = item.getItemId();
        if(v==R.id.home)
        {
            getSupportActionBar().setTitle("Home");
            Fragment fragment = new HomeFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
        else if (v==R.id.dash_board)
        {
            getSupportActionBar().setTitle("Dashboard");
            Fragment fragment = new DashboardFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
   return true;
    }
};

这是一个示例
如果您想详细了解,请点击此处

First of all we do this in the MainActivity

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int v = item.getItemId();
        if(v==R.id.home)
        {
            getSupportActionBar().setTitle("Home");
            Fragment fragment = new HomeFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
        else if (v==R.id.dash_board)
        {
            getSupportActionBar().setTitle("Dashboard");
            Fragment fragment = new DashboardFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
   return true;
    }
};

This is a example here
If you want to know in detail then click here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文