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

He was the "last man who knew everything" and died at the age of 55

This article is the preface written by Royal Astr...

Game apps on the App Store paid list were punished!

Friends who are familiar with the paid list know ...

Debate: Is Yang Yuanqing a qualified Lenovo CEO?

An article in a certain self-media titled "T...

How much does it cost to develop a Zengcheng e-commerce mini program?

Although mini programs have been around for quite...

Difference between FragmentPagerAdapter and FragmentStatePagerAdapter

FragmentPagerAdapter and FragmentStatePagerAdapte...

Two Jurassic "mice" can rewrite the structure of mammalian evolution

Researchers from the Institute of Vertebrate Pale...

Online traffic diversion skills for training institutions

When chatting with me, many heads of offline inst...