首页 > 解决方案 > 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>

标签: javaandroid

解决方案


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

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

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

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

推荐阅读