You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BottomNavigationView首项始终高亮:切换其他导航项无法正常高亮

解决BottomNavigationView首个Item始终高亮的问题

这个问题我之前也踩过坑,核心原因其实很直白——你的onNavigationItemSelected方法没有返回正确的布尔值,导致系统没法更新导航栏的选中状态。

问题拆解

BottomNavigationView的OnNavigationItemSelectedListener要求onNavigationItemSelected方法返回true,用来告诉系统:“我已经处理完这个选中事件了,请更新对应Item的高亮状态”。你的代码只完成了Fragment切换的逻辑,但最后没有返回正确值(默认会返回false),系统就不会改变选中状态,只会一直保持第一个Item的高亮。

修复代码

修改你的实现,确保在完成Fragment切换后返回true,也可以显式调用item.setChecked(true)来强制同步选中状态(虽然返回true通常就足够,但加上更稳妥):

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { 
    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
        Fragment fragment = null; 
        switch (item.getItemId()){ 
            case R.id.number: 
                fragment = new data(...); // 你的Fragment实例化逻辑
                break;
            // 补充其他导航Item的case分支
            case R.id.your_second_item_id:
                fragment = new SecondFragment();
                break;
        }
        // 执行Fragment替换逻辑(替换你布局里的Fragment容器)
        if (fragment != null) {
            getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
        }
        // 关键步骤:通知系统更新选中状态
        item.setChecked(true);
        return true;
    }
});

额外提醒

  • 确保每个case分支都正确实例化了对应的Fragment,Fragment切换逻辑不能报错(如果切换失败,也可能间接导致状态不更新)。
  • 如果是在Fragment里使用BottomNavigationView,记得把getSupportFragmentManager()换成getChildFragmentManager()。

内容的提问来源于stack exchange,提问作者felix12340

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:13:14