Android bottom navigation bar implementation (I) BottomNavigationBar

Android bottom navigation bar implementation (I) BottomNavigationBar

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:

  1. mBottomNavigationBar = (BottomNavigationBar) view .findViewById(R.id.bottom_navigation_bar);
  2. mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
  3. mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
  4.  
  5. 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))
  6. .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))
  7. .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))
  8. .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))
  9. .setFirstSelectedPosition(0)
  10. .initialise();
  11.  
  12. mBottomNavigationBar.setTabSelectedListener(this);
  13.  
  14. setDefaultFragment();

Tab switching:

  1. @Override
  2. public void onTabSelected( int position) {
  3. FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
  4.  
  5. switch (position) {
  6. case 0:
  7. if (mHomeFragment == null ) {
  8. mHomeFragment = HomeFragment.newInstance(getString(R.string.item_home));
  9. }
  10. beginTransaction.replace (R.id.sub_content, mHomeFragment);
  11. break;
  12. case 1:
  13. if (mLocationFragment == null ) {
  14. mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location));
  15. }
  16. beginTransaction.replace (R.id.sub_content, mLocationFragment);
  17. break;
  18. case 2:
  19. if (mLikeFragment == null ) {
  20. mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like));
  21. }
  22. beginTransaction.replace (R.id.sub_content, mLikeFragment);
  23. break;
  24. case 3:
  25. if (mPersonFragment == null ) {
  26. mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person));
  27. }
  28. beginTransaction.replace (R.id.sub_content, mPersonFragment);
  29. }
  30. beginTransaction.commit ();
  31.  
  32. }

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.

<<:  Using Flink as an example to dispel six common myths about stream processing

>>:  Android bottom navigation bar implementation (Part 2) RadioGroup

Recommend

How far are traditional TV brands from experiencing the "Nokia tragedy"?

Even though Ma Huateng once publicly promised tha...

New discovery! The origin story of angiosperms hidden in "stones"

Produced by: Science Popularization China Author:...

Nature: In China, people used pigments 40,000 years ago!

Author: Hu Minqi "Stone flakes" produce...

First experience of watching TV series while charging

With the popularity of mobile phones, mobile powe...

How to retain new users?

Why did we lose the users we worked so hard to ac...

Advanced Sports Nutrition

Course Catalog ├──Diet for weight loss | ├──Nutri...

The popularity of "Honghuang Girl" is a microcosm of the era of sports fission

This Olympics was a bit dull, and the audience wh...