我的底部导航视图不起作用。当我单击项目时,我的片段无法加载。
这是我底部导航视图的活动
public static void enableNavigation(Context context, final BottomNavigationViewEx view, final FragmentManager supportFragmentManager){
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new FragmentBlog();
loadFragment(fragment);
return true;
case R.id.nav_notification:
fragment = new FragmentNotification();
loadFragment(fragment);
return true;
}
return false;
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = fragment.getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
我正在使用此库link
答案 0 :(得分:1)
尝试
Fragment fragment;
boolean valToReturn = false
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
valToReturn = true;
break;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
valToReturn = true;
break;
case R.id.nav_blog:
fragment = new FragmentBlog();
valToReturn = true;
break;
case R.id.nav_notification:
fragment = new FragmentNotification();
valToReturn = true;
break;
}
loadFragment(fragment);
return valToReturn;
答案 1 :(得分:0)
Java
break
用于切换语句。它破坏了电流 程序在指定条件下的状态。
您错过添加 break;
了。
演示结构
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragment = FragmentMarker();
loadFragment(fragment);
break;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
break;
}
.......