BottomNavigation视图中所选项目的标签未更改

时间:2019-11-23 10:35:02

标签: java android

我正在尝试实现简单的BottomNavigationView。片段会根据所单击的项目正确运行,但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>

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

原来setOnNavigationItemSelectedListener应该返回true。