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



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

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

接下来,我进入了实现导航项目electlistner的主要活动,但是在完成此方法后没有任何错误,我的底部导航不起作用。

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;
    }
});

我的菜单项 :

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

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

请确保添加以下代码

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

 NavigationUI.setupWithNavController(navView, navController);  

在下面尝试,它对我来说很好用

菜单 XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_home"
    android:icon="@drawable/ic_home_blue_48dp"
    android:title="Home" />
<item
    android:id="@+id/action_menu"
    android:icon="@drawable/ic_apps_black_24dp"
    android:title="Menu"
     />
<item
    android:id="@+id/action_msg"
    android:icon="@drawable/ic_chat_black_24dp"
    android:title="Message Inbox"
   />
</menu>

.xml

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    app:itemIconTint="@drawable/bottom_nav_colors"
    app:itemTextColor="@drawable/bottom_nav_colors"
    app:menu="@menu/bottom_navigation_items"/>

底部侦听器

BottomNavigationView bottomNavigationView;
bottomNavigationView.setOnNavigationItemSelectedListener(new 
BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item)
        {
            Fragment selectedFragment = null;
            switch (item.getItemId())
            {
                case R.id.action_home:
                   //perform action
                    break;
                case R.id.action_menu:
                    //perform action
                    break;
                case R.id.action_msg:
                   //perform action
                    break;
            }
            FragmentTransaction transaction = 
    getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, selectedFragment);
            transaction.commit();
            return true;
        }
    });

替换这个

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

通过这个

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

最新更新