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

The "Beijing Man" who disappeared in the war tells us the value of peace

Beijing is a city with a history of more than 3,0...

How to operate Toutiao account? New to operating a Toutiao account?

Toutiao is one of the important platforms of self...

Xi'an tea service is the best and highest quality

Xi'an High-Quality Tea Tasting Selection (WeC...

Get APP product analysis

Before fully understanding this app, everyone tho...

4 reasons why Microsoft Surface is doomed

The technology website Computerworld recently rep...

Summary of 7 commonly used sorting algorithms in Java

I have been free for a while, so I took some time...

What brand advantages are needed to use low-price strategies for marketing?

Many people believe that if the product has the s...

A complete collection of classic advertising slogans from all industries

A creative slogan or advertising copy can often t...

New media operation: How to create a brand super symbol

In daily life, we often have this experience: mos...

Why does it take so long for Apple's AirPods headphones?

If we talk about Apple's most entangled access...