Navigation+BottomNavigationView的使用(2个以上tab)

本篇主要介绍使用2个以上的tab页时的实现;

  1. 接着上篇建立的FirstFragment.java, SecondFragment.java
  2. 导航视图文件nav_fragment.xml修改成:

 
    
        
        
    
    
      
      
    
    
      
      
    

  1. 在res下新建menu文件夹,并新建menu_navigation.xml


    

    
    

bottom_sel.xml 切换icon的状态,选中与非选中时呈现不一样的图片:



    
    

  1. MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private NavigationController navController;
    private BottomNavigationView bottomNavView;
    private MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initNav();
  
    }

    private void initNav(){
         navController = Navigation.findNavController(this,R.id.fragment);
        bottomNavView = (BottomNavigationView) findViewById(R.id.bottomnavigationview);
        NavigationUI.setupWithNavController(bottomNavView,navController );
        bottomeNavView.setItemIconTintList(null);
        bottomNavView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.menu_fragment1:
                //当该项已经被选中时,不执行任何操作
                if(item.isChecked()) {
                  return true;
                }
               if (bottomNavView.getSelectedItemId() == R.id.menu_fragment2) {
                   navController.navigate(R.id.action_secondFragment_to_first);
               } else if ((bottomNavView.getSelectedItemId() == R.id.menu_food) {
                   navController.navigate(R.id.action_foodFragment_to_first);
               }
                menuItem.setChecked(true);
                break;
            case R.id.menu_fragment2:
                if(item.isChecked()) {
                  return true;
                }
                if (bottomNavView.getSelectedItemId() == R.id.menu_fragment1) {
                   navController.navigate(R.id.action_firstFragment_to_second);
               } else if ((bottomNavView.getSelectedItemId() == R.id.menu_food) {
                   navController.navigate(R.id.action_foodFragment_to_second);
               }
               menuItem.setChecked(true);
                break;
           case R.id.menu_food:
                if(item.isChecked()) {
                  return true;
                }
                if (bottomNavView.getSelectedItemId() == R.id.menu_fragment1) {
                   navController.navigate(R.id.action_firstFragment_to_food);
               } else if ((bottomNavView.getSelectedItemId() == R.id.menu_fragment2) {
                   navController.navigate(R.id.action_secondFragment_to_food);
               }
               menuItem.setChecked(true);
                break;
        }
        return false;
    }

}
  1. 布局文件
    activity_main.xml


 
   


    

你可能感兴趣的:(Navigation+BottomNavigationView的使用(2个以上tab))