BottomNavigationView(Tab的按钮)

碰到一个很好的Tab按钮的写法所以记录一下

布局

   



在res目录下创建一个menu



    
    
    
    
    


Java代码

 mVp.addOnPageChangeListener (new ViewPager.OnPageChangeListener () {
          @Override
          public void onPageScrolled(int i, float v, int i1) {

          }

          @Override
          public void onPageSelected(int i) {
              mBnv.getMenu ().getItem (i).setChecked (true);
          }

          @Override
          public void onPageScrollStateChanged(int i) {

          }
      });
      // 为bnv设置选择监听事件
      mBnv.setOnNavigationItemSelectedListener (new BottomNavigationView.OnNavigationItemSelectedListener () {
          @Override
          public boolean onNavigationItemSelected(@NonNull MenuItem item) {
              switch (item.getItemId ()) {
                  case R.id.home:
                      // 跳转到对应的页面
                      mVp.setCurrentItem (0);
                      break;
                  case R.id.guide:
                      mVp.setCurrentItem (1);
                      break;
                  case R.id.msg:
                      mVp.setCurrentItem (2);
                      break;
                  case R.id.person:
                      mVp.setCurrentItem (3);
                      break;
                  case R.id.sett:
                      mVp.setCurrentItem (4);
                      break;
              }
              // 这里必须返回true才能响应点击事件
              return true;
          }
      });
问题注意 :

底部Bottom选项,最少为3项,超过3项之后,BottomNavigationView会自动使用本身控件的自带动画属性,同时最多好像为5项,目前没有进行验证。
如果想要图标本身的颜色就加问题归纳

mBottomNV = (BottomNavigationView) findViewById(R.id.bottom_navigation);
mBottomNV.setItemIconTintList(null);

问题 :当底部Button超过三个之后,自带动画效果
需求 :取消原有动画效果,常规显示

解决方式 :

创建BottomNavigationViewHelper类,进行disableShiftMode方法调用

public class BottomNavigationViewHelper {
    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt (0);
        try {
            Field shiftingMode = menuView.getClass ().getDeclaredField ("mShiftingMode");
            shiftingMode.setAccessible (true);
            shiftingMode.setBoolean (menuView, false);
            shiftingMode.setAccessible (false);
            for (int i = 0; i < menuView.getChildCount (); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt (i);
                //noinspection RestrictedApi
                item.setShifting (false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked (item.getItemData ().isChecked ());
            }
        } catch (NoSuchFieldException e) {
            Log.e ("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e ("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}

设置BottomNavigationView展示类型

        BottomNavigationView mBv = (BottomNavigationView) findViewById(R.id.bv);
        //调用上面的类进行设置
        BottomNavigationViewHelper.disableShiftMode(mBv);

你可能感兴趣的:(BottomNavigationView(Tab的按钮))