底部导航 在导航项上选择侦听器不起作用

Har*_*ari 5 java android android-layout bottomnavigationview android-bottomnav

我想为我的应用程序构建底部导航,并在完成底部导航后。布局。

<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/Bottomnav"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:layout_gravity="bottom"
            android:background="#FFFFFF"
            app:menu="@menu/bottomnav_menus">

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

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

接下来,我进入了我的主要活动来实现 onnavigationitemselectlistner,但是在没有任何错误的情况下执行此方法后,我的底部导航。不工作。

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId())
        {
            case R.id.live:
                Intent intent = new Intent(MainActivity.this, LiveScore.class);
                startActivity(intent);
                return true;
        }
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

我的菜单项:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:title=""
        android:id="@+id/home"
        android:icon="@drawable/baseline_home_black_24dp"
        android:orderInCategory="1"/>

    <item
        android:title=""
        android:id="@+id/live"
        android:icon="@drawable/baseline_live_tv_black_24dp"
        android:orderInCategory="2"/>
    <item
        android:title=""
        android:id="@+id/video"
        android:icon="@drawable/baseline_video_library_black_24dp"
        android:orderInCategory="3"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

如果您需要任何其他信息,请发表评论。

小智 9

确保您添加以下代码

navView.setOnNavigationItemSelectedListener(new ...);
navView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
            @Override
            public void onNavigationItemReselected(@NonNull MenuItem item) {
                Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
            }
        });
Run Code Online (Sandbox Code Playgroud)

 NavigationUI.setupWithNavController(navView, navController);  
Run Code Online (Sandbox Code Playgroud)


Moh*_*yed 1

替换这个

  switch (item.getItemId())
        {
            case R.id.live:
                Intent intent = new Intent(MainActivity.this, LiveScore.class);
                startActivity(intent);
                return true;

        }
        return false;
Run Code Online (Sandbox Code Playgroud)

这样

  switch (item.getItemId())
            {
                case R.id.live:
                       Intent intent = new Intent(MainActivity.this,LiveScore.class);
                    startActivity(intent);
                    break;
            }
            return true;
Run Code Online (Sandbox Code Playgroud)