未为底部导航栏调用onNavigationItemSelected

时间:2017-11-11 19:17:10

标签: java android bottomnavigationview android-support-design

我正在尝试实现一个底部导航栏,可在单击导航项时更改片段。但是,当我单击导航栏项时,片段不会更改。使用onNavigationItemSelected,我注意到startFeedFragment未被调用。 如何解决此问题?

要注意,startScheduleFragmentstartSettingsFragment和& 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); } } 以相同的方式实现,它们适用于工具栏中的按钮。我还引用了这个tutorialquestion来寻求帮助。

MainActivity.java

    <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>

xml文件中的代码段

public static boolean diff(int[] ar) {
        boolean result = true;
        for (int i = 0; i < ar.length; i++) {
            for (int j = 0; j < ar.length; j++) {
                if (Math.abs(ar[i] - ar[j]) > 3) {
                    result = false;
                }
            }
        }
        return result;
}

enter image description here

2 个答案:

答案 0 :(得分:0)

解决!这是因为我已将菜单中的按钮设置为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>

答案 1 :(得分:0)

对我来说,问题是当我的活动主布局是 LinearLayout 时,所以我通过将其更改为 RelativeLayout 来解决它,我完全不知道它为什么起作用,因为 {{1 }} 布局只回复元素位置,而不是它的视图或其他东西,但它正在工作。

RelativeLayout