The use of the BottomNavigationBar control has been written before, so I will not repeat it here. For details, please refer to the use of BottomNavigationBar. Here is the code directly: Initialization and related settings: - mBottomNavigationBar = (BottomNavigationBar) view .findViewById(R.id.bottom_navigation_bar);
- mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
- mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
-
- mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.home_fill, getString(R.string.item_home)).setInactiveIconResource(R.drawable.home).setActiveColorResource(R.color.colorPrimary).setInActiveColorResource(R.color.black_1))
- .addItem(new BottomNavigationItem(R.drawable.location_fill, getString(R.string.item_location)).setInactiveIconResource(R.drawable.location).setActiveColorResource(R.color.colorPrimary).setInActiveColorResource(R.color.black_1))
- .addItem(new BottomNavigationItem(R.drawable.like_fill, getString(R.string.item_like)).setInactiveIconResource(R.drawable. like ).setActiveColorResource(R.color.colorPrimary).setInActiveColorResource(R.color.black_1))
- .addItem(new BottomNavigationItem(R.drawable.person_fill, getString(R.string.item_person)).setInactiveIconResource(R.drawable.person).setActiveColorResource(R.color.colorPrimary).setInActiveColorResource(R.color.black_1))
- .setFirstSelectedPosition(0)
- .initialise();
-
- mBottomNavigationBar.setTabSelectedListener(this);
-
- setDefaultFragment();
Tab switching: - @Override
- public void onTabSelected( int position) {
- FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
-
- switch (position) {
- case 0:
- if (mHomeFragment == null ) {
- mHomeFragment = HomeFragment.newInstance(getString(R.string.item_home));
- }
- beginTransaction.replace (R.id.sub_content, mHomeFragment);
- break;
- case 1:
- if (mLocationFragment == null ) {
- mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location));
- }
- beginTransaction.replace (R.id.sub_content, mLocationFragment);
- break;
- case 2:
- if (mLikeFragment == null ) {
- mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like));
- }
- beginTransaction.replace (R.id.sub_content, mLikeFragment);
- break;
- case 3:
- if (mPersonFragment == null ) {
- mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person));
- }
- beginTransaction.replace (R.id.sub_content, mPersonFragment);
- }
- beginTransaction.commit ();
-
- }
Note: These articles do not have too much text description, because these things are not very difficult, and they are commonly used. I believe that many people are familiar with them. It is nonsense to say more. It is clearer to read the code directly. |