BottomNavigationView.setOnItemSelectedListener失效问题求助
核心问题与修复步骤:
1. 修复Material组件依赖的无效版本
你的Material依赖存在语法错误,1..0是无效版本,会导致组件功能异常,直接影响BottomNavigationView的事件监听:
// 原错误依赖 implementation 'com.google.android.material:material:1..0' // 修改为有效版本(建议与AppCompat版本匹配) implementation 'com.google.android.material:material:1.4.0'
2. 解决布局层级遮挡导致的点击拦截
Activity布局中,include的content_main设置了固定高度850dp,在多数设备上会覆盖底部的BottomNavigationView,导致点击事件被上层View拦截,无法触发监听:
// 原错误布局 <include layout="@layout/content_main" android:layout_width="wrap_content" android:layout_height="850dp" /> // 修改为自适应布局,避免遮挡底部导航 <include layout="@layout/content_main" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
如果content_main中没有AppBarLayout,也可以通过添加底部边距避开导航栏:
android:layout_marginBottom="?attr/actionBarSize"
3. 额外验证项
- 确认
bottom_navigation_menu中的item ID与监听代码中的R.id.nav_home、R.id.nav_summary、R.id.nav_notifications完全一致,无拼写错误; - 检查
replaceFragment方法中使用的R.id.nav_host_fragment是否存在于content_main布局中,容器ID错误会导致Fragment切换无视觉反馈。
内容的提问来源于stack exchange,提问作者uyasd
相关产品推荐
相关产品推荐

