底部导航栏未调用 onNavigationItemSelected

onNavigationItemSelected not being called for Bottom Navigation Bar

我正在尝试实现一个底部导航栏,它会在单击导航项时更改片段。但是,当我单击导航栏项目时,片段不会改变。使用 log.d,我注意到 onNavigationItemSelected 没有被调用。 我该如何解决这个问题?

请注意,startFeedFragmentstartScheduleFragmentstartSettingsFragment 的实现方式相同,它们适用于工具栏中的按钮。我还参考了这个 and this 寻求帮助。

MainActivity.java

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setUpRecycler();
        startFeedFragment();

        BottomNavigationView bottomNavBar = (BottomNavigationView) findViewById(R.id.navigation);
        bottomNavBar.bringToFront();
        bottomNavBar.setOnNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Log.d("NAV BAR", "onNavigationItemSelected hit");
        switch (item.getItemId()) {
            case R.id.feedMenu:
                startFeedFragment();
                Log.d("NAV BAR", "feed");
                break;
            case R.id.myScheduleMenu:
                startScheduleFragment();
                Log.d("NAV BAR", "schedule");
                break;
            case R.id.settingsMenu:
                startSettingsFragment();
                Log.d("NAV BAR", "settings lol");
                break;
            default:
                Log.d("NAV BAR", "false");
                return false;
        }
        return true;
    }

   /**
     * Switches out the fragment for {@link FeedFragment} and sets an appropriate title. startScheduleFragmens & startSettingsFragment are implemented the same way
     */
    private void startFeedFragment()
    {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragmentContainer, new FeedFragment());
        transaction.commit();
        getSupportActionBar().setTitle(R.string.title_fragment_feed);
    }
}

来自 xml 文件的片段

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/navigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@android:color/white"
            app:itemIconTint="@color/colorPrimaryDark"
            app:itemTextColor="@color/colorPrimaryDark"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_nav"
            android:elevation="8dp"/>
    </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

已解决!这是因为我将菜单中的按钮设置为android:enabled="false"。这意味着它们不能被按下,这就是 onNavigationItemSelected 没有被调用的原因。我将所有按钮设置为 android:enabled="true" 来解决这个问题。

bottom_nav.xml

的正确代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/bottom_nav_my_schedule"
        android:title="@string/menu_my_schedule"
        android:icon="@drawable/ic_event_available_white_24dp"
        android:enabled="true"/> <!--make sure enabled is set to true!-->
    <item android:id="@+id/bottom_nav_feed"
        android:title="@string/menu_feed"
        android:icon="@drawable/ic_view_list_white_24dp"
        android:enabled="true" /> <!--make sure enabled is set to true!-->
    <item android:id="@+id/bottom_nav_settings"
        android:title="@string/menu_settings"
        android:icon="@drawable/ic_settings_white_24dp"
        android:enabled="true"/> <!--make sure enabled is set to true!-->
</menu>

至于我的问题是当我的 activity 主布局是 LinearLayout 时,所以我通过将其更改为 RelativeLayout 来解决它,我完全不知道为什么会这样工作,因为 RelativeLayout 布局只回复元素位置,而不是它的视图或其他东西,但它正在工作。

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
  
  <android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
  
  <android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemBackground="@drawable/selector"
    android:background="@color/white"
    app:menu="@menu/menu_bottom"
    app:itemIconTint="@drawable/menu_trainings"
    app:labelVisibilityMode="unlabeled"
    />
    
</RelativeLayout>