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

The bathroom is so small, why is the WiFi signal always so bad?

The bathroom is so small, why is the WiFi signal ...

8 ways to drive traffic to public accounts through short videos

Friends who work on short video columns are no st...

Common user incentive methods for event operations!

In the definition of a service platform, as the p...

12 ways to activate your social network

Many communities die soon after they are establis...

They produce 20% of Earth's oxygen, but are disappearing

Most life on Earth depends on oxygen to survive. ...

Xiaomi 4 frame violence test: Is the paint really peeling off?

Yesterday, Weibo user @哲野狂人 said that the Xiaomi M...

How to anger a copywriter in one sentence?

"It's just writing some words, everyone c...

The first electron beam was successfully accelerated. What exactly is HEPS?

March 14, 2023 The 13th Five-Year Plan for Nation...

If these 3 points are not clear, even the marketing gods will fail!

The simplest logic for marketing is to ask yourse...