Android bottom navigation bar implementation (IV) TabLayout+ViewPager

Android bottom navigation bar implementation (IV) TabLayout+ViewPager

Here is a brief record of implementing the Android bottom navigation bar through TabLayout+ViewPager.

layout

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. xmlns:app= "http://schemas.android.com/apk/res-auto"  
  4. android:layout_width= "match_parent"  
  5. android:layout_height= "match_parent"  
  6. android:orientation= "vertical" >
  7.  
  8. <include layout= "@layout/fragment_content" />
  9.  
  10. <LinearLayout
  11. android:layout_width= "match_parent"  
  12. android:layout_height= "match_parent"  
  13. android:orientation= "vertical" >
  14.  
  15.  
  16. < android.support.v4.view.ViewPager
  17. android:id= "@+id/view_pager"  
  18. android:layout_width= "match_parent"  
  19. android:layout_height= "0dp"  
  20. android:layout_weight= "1" ></android.support.v4. view .ViewPager>
  21.  
  22. <android.support.design.widget.TabLayout
  23. android:id= "@+id/tab_layout"  
  24. android:layout_width= "match_parent"  
  25. android:layout_height= "56dp"  
  26. app:tabBackground= "@color/white"  
  27. app:tabIndicatorHeight= "0dp"  
  28. app:tabSelectedTextColor= "@color/colorPrimary"  
  29. app:tabTextAppearance= "@style/tabTextSizeStyle"  
  30. app:tabTextColor= "@color/black_1" ></android.support.design.widget.TabLayout>
  31. </LinearLayout>
  32. </RelativeLayout>

Code

  1. mViewPager = (ViewPager) view .findViewById(R.id.view_pager);
  2. mTabLayout = (TabLayout) view .findViewById(R.id.tab_layout);
  3. initTabList();
  4. mAdapter = new TabLayoutFragmentAdapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs);
  5. mViewPager.setAdapter(mAdapter);
  6. mViewPager.setCurrentItem(0);
  7. mTabLayout.setupWithViewPager(mViewPager);
  8. mTabLayout.setTabMode(TabLayout.MODE_FIXED); //Set the mode of TabLayout
  9. for ( int i = 0; i < mTabLayout.getTabCount(); i++) {
  10. mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i));
  11. }
  12. mTabLayout.addOnTabSelectedListener(this); //Set the selected listener of TabLayout

What needs to be noted here is the use of TabLayout. TabLayout is used in conjunction with ViewPager. Use mTabLayout.setupWithViewPager(mViewPager); to connect the two. In addition, customView is used here. Of course, TabLayout's own methods can also achieve the icon+text effect. That is, the effect: TabLayout + ViewPager 2

Display the Tab key effect according to the Tab selection state

  1. @Override
  2. public void onTabSelected(TabLayout.Tab tab) {
  3. setTabSelectedState(tab);
  4. }
  5.  
  6. @Override
  7. public void onTabUnselected(TabLayout.Tab tab) {
  8. setTabUnSelectedState(tab);
  9. }
  10.  
  11.  
  12. @Override
  13. public void onTabReselected(TabLayout.Tab tab) {
  14.  
  15. }
  16.  
  17.  
  18.  
  19.  
  20. private void setTabSelectedState(TabLayout.Tab tab) {
  21. View customView = tab.getCustomView();
  22. TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text);
  23. ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon);
  24. tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
  25. String s = tabText.getText().toString();
  26. if (getString(R.string.item_home).equals(s)) {
  27. tabIcon.setImageResource(R.drawable.home_fill);
  28. } else if (getString(R.string.item_location).equals(s)) {
  29. tabIcon.setImageResource(R.drawable.location_fill);
  30. } else if (getString(R.string.item_like).equals(s)) {
  31. tabIcon.setImageResource(R.drawable.like_fill);
  32. } else if (getString(R.string.item_person).equals(s)) {
  33. tabIcon.setImageResource(R.drawable.person_fill);
  34. }
  35. }
  36.  
  37. private void setTabUnSelectedState(TabLayout.Tab tab) {
  38. View customView = tab.getCustomView();
  39. TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text);
  40. ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon);
  41. tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.black_1));
  42. String s = tabText.getText().toString();
  43. if (getString(R.string.item_home).equals(s)) {
  44. tabIcon.setImageResource(R.drawable.home);
  45. } else if (getString(R.string.item_location).equals(s)) {
  46. tabIcon.setImageResource(R.drawable.location);
  47. } else if (getString(R.string.item_like).equals(s)) {
  48. tabIcon.setImageResource(R.drawable. like );
  49. } else if (getString(R.string.item_person).equals(s)) {
  50. tabIcon.setImageResource(R.drawable.person);
  51. }
  52. }

There is no need to set defaultFragment here, TabLayout will display the first one by default;

In addition, here is also posted the code to use TabLayout's own method to achieve the effect

It is worth mentioning that the built-in method cannot customize the spacing between icon and text. It is very convenient to use, but it may not be the size you require. I have not studied the source code in depth. If anyone knows how to change this built-in method, please let me know.

  1. mViewPager = (ViewPager) view .findViewById(R.id.view_pager);
  2. mTabLayout = (TabLayout) view .findViewById(R.id.tab_layout);
  3. initTabList();
  4. mAdapter = new TabLayoutFragment2Adapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs);
  5. mViewPager.setAdapter(mAdapter);
  6. mViewPager.setCurrentItem(0);
  7. mTabLayout.setupWithViewPager(mViewPager);
  8. mTabLayout.setTabMode(TabLayout.MODE_FIXED);
  9. // for ( int i = 0; i < mTabLayout.getTabCount(); i++) {
  10. // mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i));
  11. // }
  12. mTabLayout.addOnTabSelectedListener(this);
  13. // mViewPager.setCurrentItem(0);
  14. mTabLayout.getTabAt(0).setIcon(R.drawable.home_fill); //Own method to add icon
  15. mTabLayout.getTabAt(1).setIcon(R.drawable.location);
  16. mTabLayout.getTabAt(2).setIcon(R.drawable. like );
  17. mTabLayout.getTabAt(3).setIcon(R.drawable.person);

Tab Switching

  1. @Override
  2. public void onTabSelected(TabLayout.Tab tab) {
  3. // setTabSelectedState(tab);//This is also not necessary
  4. resetTabIcon();
  5. int position = tab.getPosition();
  6. Log.e( "Kevin" , "position--->" + position);
  7. switch (position) {
  8. case 0:
  9. tab.setIcon(R.drawable.home_fill);
  10. break;
  11. case 1:
  12. tab.setIcon(R.drawable.location_fill);
  13. break;
  14. case 2:
  15. tab.setIcon(R.drawable.like_fill);
  16. break;
  17. case 3:
  18. tab.setIcon(R.drawable.person_fill);
  19. break;
  20.  
  21. }
  22. }
  23.  
  24.  
  25. private void resetTabIcon() {
  26. mTabLayout.getTabAt(0).setIcon(R.drawable.home);
  27. mTabLayout.getTabAt(1).setIcon(R.drawable.location);
  28. mTabLayout.getTabAt(2).setIcon(R.drawable. like );
  29. mTabLayout.getTabAt(3).setIcon(R.drawable.person);
  30. }

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.

<<:  Android bottom navigation bar implementation (Part 3) TextView+LinearLayout

>>:  2016 ZOL Annual Technology Product Awards Ceremony Held in Beijing

Recommend

Community operation: How to build a highly active user community?

Social media is an important means of attracting ...

How to promote online for free (low cost) (Part 1)

Recently, many people have asked me a question, h...

About Tint Color Properties

Tint Color After iOS 7, UIView added a new tintCo...

2023 Enterprise Digital Transformation Capability Survey

The digital transformation of an enterprise is a ...

One article to understand: What exactly is updated in Google Android Q

In the early morning of May 8, the 2019 Google I/...

Offline building advertising targeted by BAT

Highlights: 1. With the investment of giants, off...

What is the essence of bidding promotion?

The promotion effect is abnormal, the data is uns...