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

Analysis of Tik Tok’s refined operation and promotion techniques!

The author of this article analyzes Douyin’s pers...

AMD details Radeon RX 400 series naming conventions, confirms RX 490

AMD has released the first graphics card of the Po...

Telecom network fraud is actually a script-killing

This article was first published by Hunzhi (WeCha...

Huawei files lawsuit in the US: ban is "legislation instead of trial"

According to CCTV News Client on the 29th, Huawei...

A Complete List of English Terms for Advertising

When using professional tools, we can always retu...

QR code promotion, 6 invalid forms to avoid

When doing new media promotion , many operators d...

Do foods labeled “0 calories” really have no energy at all?

As the weather warms up, have the weight loss arm...

Is Linux right for you?

Linux is not for everyone -- for many users, Wind...

Audi skysphere concept car: the future is here

On August 10, 2021, the first model of Audi's...