Android 底部导航栏BottomNavigationView的使用

1.BottomNavigationView是support desigin包下的控件,用于底部导航~

①使用之前在App的build.gradle中添加依赖

com.android.support:design:27.1.1

②接下来布局文件




    

    

布局包含内容区域和导航栏,内容区域framelayout是用来承载不同的fragment

#FFFFFF
bottom_nav_item_selector:





    

bottom_nav_menu:



    
    
    
    

监听BottomNavigationView状态发生变化时,fragment的切换

public class MainActivity extends AppCompatActivity {

   
    @BindView(R.id.framelayout)
    FrameLayout framelayout;
    @BindView(R.id.bottom_nav_view)
    BottomNavigationView bottomNavView;
   

    private List mFragments;
    HomePageFragment homePageFragment;
    KnowledgeFragment knowledgeFragment;
    NavigationFragment navigationFragment;
    ProjectFragment projectFragment;
    FragmentManager mSupportFragmentManager;
    UserDialogFragment userDialogFragment;
    SearchFragment searchFragment;
    FragmentTransaction mTransaction;
    private int mLastIndex; //记录点击上一个fragment的索引值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initToobar();
        initDrawerLayout();
        initBottomNavigationView();
        initFragment();
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        homePageFragment = HomePageFragment.newInstance(null, null);
        knowledgeFragment = KnowledgeFragment.newInstance(null, null);
        navigationFragment = NavigationFragment.newInstance(null, null);
        projectFragment = ProjectFragment.newInstance(null, null);
        userDialogFragment = UserDialogFragment.newInstance(null, null);
        searchFragment=SearchFragment.newInstance(null,null);

        mFragments.add(homePageFragment);
        mFragments.add(knowledgeFragment);
        mFragments.add(navigationFragment);
        mFragments.add(projectFragment);
        mFragments.add(userDialogFragment);
        mFragments.add(searchFragment);
        //初始化展示HomePageFragment
        setFragmentPosition(0,getString(R.string.tv_tab_home));
    }


    private void initBottomNavigationView() {
        BottomNavigationViewHelper.disableShiftMode(bottomNavView);//解决当item大于三个时,非平均布局问题
        bottomNavView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.tab_home_page: //首页
                        setFragmentPosition(0,getString(R.string.tv_tab_home));
                        break;
                    case R.id.tab_knowledge_hierarchy: //知识体系
                        setFragmentPosition(1,getString(R.string.tv_knowledge_hierarchy));
                        break;
                    case R.id.tab_navigation: //导航
                        setFragmentPosition(2,getString(R.string.tv_navigation));
                        break;
                    case R.id.tab_project://项目
                        setFragmentPosition(3,getString(R.string.tv_project));
                        break;
                }
                return true;
            }
        });
    }

    public void setFragmentPosition(int position,String toobarTitle) {
        title.setText(toobarTitle);
        mSupportFragmentManager = this.getSupportFragmentManager();
        mTransaction = mSupportFragmentManager.beginTransaction();
        Fragment currentFragment = mFragments.get(position);
        Fragment lastFragement = mFragments.get(mLastIndex);
        mLastIndex = position;
        mTransaction.hide(lastFragement);
        if (!currentFragment.isAdded()) {
            //mTransaction.remove(currentFragment).commit();
            mTransaction.add(R.id.framelayout, currentFragment);
        }
        mTransaction.show(currentFragment);
        mTransaction.commitAllowingStateLoss();
    }

}

使用到的工具类:


/**
 * Created by ${wcystart}
 * date:on 2019/1/29
 * description:解决当导航栏按钮个数大于三个时,分布非平均布局问题
 */

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.setShiftingMode(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);
        }
    }
}

效果图:

Android 底部导航栏BottomNavigationView的使用_第1张图片   Android 底部导航栏BottomNavigationView的使用_第2张图片

Android 底部导航栏BottomNavigationView的使用_第3张图片     Android 底部导航栏BottomNavigationView的使用_第4张图片

你可能感兴趣的:(android,UI)