gpt4 book ai didi

java - BottomNavigationview 所选项目的级别未更改

转载 作者:行者123 更新时间:2023-12-01 20:00:45 26 4
gpt4 key购买 nike

我正在尝试实现简单的 BottomNavigationView。 fragment 根据单击的项目正确运行,但 setOnNavigationItemSelectedListener 似乎不会为我的 BottomNavigationView 中的标签调用。例如,属性 app:labelVisiilityMode=selected 不起作用,无论选择哪个项目,r 仍然选择第一个项目。这是我的代码:

    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>

最佳答案

我认为,您在替换 fragment 时使用了错误的 id。

ft.replace(R.id.content_frame, fragment);

我在您的 xml 文件中没有找到任何名为 content_frame 的 ID。因此,您可以使用 frame_layout 而不是 content_frame。

  ft.replace(R.id.frame_layout, fragment);

关于java - BottomNavigationview 所选项目的级别未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59006706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com