java - BottomNavigation 视图选定项的标签未更改
问题描述
我正在尝试实现简单的 BottomNavigationView。片段根据单击的项目正确运行,但似乎没有为我的 BottomNavigationView 中的标签调用 setOnNavigationItemSelectedListener。例如属性 app:labelVisiilityMode=selected 不起作用,并且无论选择什么项目,仍然会选择第一个项目。这是我的代码:
private void configureBottoNavigationView() {
bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
Bundle args;
switch (item.getItemId()) {
case R.id.action_item1:
fragment = new FragmentList();
args = new Bundle();
args.putString("number_of_cols", prefList);
args.putInt("b_nav_height", bottomNavViewHeight);
if (position != 0)
args.putInt("position", position);
fragment.setArguments(args);
break;
case R.id.action_item2:
fragment = new FragmentFav();
args = new Bundle();
args.putString("number_of_cols", prefList);
args.putInt("b_nav_height", bottomNavViewHeight);
if (position != 0)
args.putInt("position", position);
fragment.setArguments(args);
break;
case R.id.action_item3:
fragment = new FragmentSearch();
args = new Bundle();
args.putString("number_of_cols", prefList);
args.putInt("b_nav_height", bottomNavViewHeight);
if (position != 0)
args.putInt("position", position);
fragment.setArguments(args);
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
return false;
});
}
和我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="00dp"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?attr/myBackgroundColor"
app:menu="@menu/bottom_navigation_items"
app:itemTextAppearanceActive="@color/colorAccent"
app:labelVisibilityMode="selected"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color"/>
</FrameLayout>
</androidx.drawerlayout.widget.DrawerLayout>
解决方案
我认为,您在替换片段时使用了错误的 id。
ft.replace(R.id.content_frame, fragment);
我没有在您的 xml 文件中找到任何名为content_frame的 ID。因此,您可以使用frame_layout而不是 content_frame。
ft.replace(R.id.frame_layout, fragment);
推荐阅读
- android - 长按时带有 clickablespan 的 TextView 抛出异常
- inkscape - 如何在重新缩放期间保持 Inkscape 中的字体大小不变?
- php - php文件写入权限在aws ec2上被拒绝
- reactjs - 使用 react-test-renderer 进行快照测试
- ip - PowerDNS Recursor – 私有/公共 IP
- android - 需要创建一个在互联网可用时上传数据的后台服务
- java - DB H2 和控制器的集成测试
- python-2.7 - 无法单击硒中的元素
- python - 序列化子对象时出错
- python - 重复数据删除后插入记录时出现 Python Mysql 错误