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

People who doze off as soon as they sit in a car may have this health problem!

Planning and production Source: Curious Doctor Re...

Tibetan Fox: I'm so square! I did it on purpose

The Tibetan fox , with its confused and world-wea...

SoYoung User Operation Strategy Analysis Report

As modern people pay more and more attention to t...

Investing in cyclical industries and commodity cycles

Introduction to cyclical industry investment and ...

Activity operation gameplay practice

During the activity, users will go through proces...

2016 Ford Global Trends Report

We are entering an unprecedented new era. There a...

The first step to longevity: eat a little fatter!

How fat is healthy? ——A well-proportioned body is...

Hello, Server Swift

Since Apple officially released an open source ve...

Big reveal: How aerospace technology changes our lives - Clothing

In the vastness of the universe, China's spac...

During event operations, what do users generally think?

Friends around me often ask me how to organize ac...