[英]Bottom navigation On navigation item select listener not working
I want to build bottom navigation with activity for my app and after completing my bottom nav.我想为我的应用程序构建底部导航,并在完成底部导航后。 layout.布局。
<?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>
Next I gone into my Main activity for implementing onnavigationitemselectlistner, but after doing this method without any error my bottom nav.接下来,我进入了我的主要活动来实现 onnavigationitemselectlistner,但是在没有任何错误的情况下执行此方法后,我的底部导航。 not working.不工作。
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;
}
});
my menu items :我的菜单项:
<?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>
Please Comment If you want any other info.如果您需要任何其他信息,请发表评论。
make sure you are adding below code确保您添加以下代码
navView.setOnNavigationItemSelectedListener(new ...);
navView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
}
});
after后
NavigationUI.setupWithNavController(navView, navController);
this这个
Try below, it works fine for me试试下面,它对我来说很好用
menu xml菜单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 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"/>
bottom listener底层听众
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;
}
});
replace this替换这个
switch (item.getItemId())
{
case R.id.live:
Intent intent = new Intent(MainActivity.this, LiveScore.class);
startActivity(intent);
return true;
}
return false;
by this这样
switch (item.getItemId())
{
case R.id.live:
Intent intent = new Intent(MainActivity.this,LiveScore.class);
startActivity(intent);
break;
}
return true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.