Android bottom navigation bar implementation (Part 2) RadioGroup

Android bottom navigation bar implementation (Part 2) RadioGroup

Here is a brief record of the implementation of the Android bottom navigation bar through RadioGroup+Fragment.

layout:

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. android:layout_width= "match_parent"  
  4. android:layout_height= "match_parent"  
  5. android:orientation= "vertical" >
  6.  
  7. <include layout= "@layout/fragment_content" />
  8.  
  9. < View  
  10. android:layout_width= "match_parent"  
  11. android:layout_height= "1px"  
  12. android:background= "@color/grey_300"  
  13. android:elevation= "20dp" />
  14.  
  15. <RadioGroup
  16. android:id= "@+id/radio_group"  
  17. android:layout_width= "match_parent"  
  18. android:layout_height= "56dp"  
  19. android:layout_alignParentBottom= "true"  
  20. android:background= "@color/white"  
  21. android:orientation= "horizontal" >
  22.  
  23. <RadioButton
  24. android:id= "@+id/rb_home"  
  25. style= "@style/radiobutton_style"  
  26. android:checked= "true"  
  27. android:drawableTop= "@drawable/radiobutton_bg_home"  
  28. android:text= "@string/item_home"  
  29. />
  30.  
  31. <RadioButton
  32. android:id= "@+id/rb_location"  
  33. style= "@style/radiobutton_style"  
  34. android:drawableTop= "@drawable/radiobutton_bg_location"  
  35. android:text= "@string/item_location" />
  36.  
  37. <RadioButton
  38. android:id= "@+id/rb_like"  
  39. style= "@style/radiobutton_style"  
  40. android:drawableTop= "@drawable/radiobutton_bg_like"  
  41. android:text= "@string/item_like" />
  42.  
  43. <RadioButton
  44. android:id= "@+id/rb_me"  
  45. style= "@style/radiobutton_style"  
  46. android:drawableTop= "@drawable/radiobutton_bg_me"  
  47. android:text= "@string/item_person" />
  48.  
  49. </RadioGroup>
  50. </RelativeLayout>

Here the drawableTop uses a state selector

  1. <selector xmlns:android= "http://schemas.android.com/apk/res/android" >
  2. <item android:drawable= "@drawable/home_fill" android:state_checked= "true" />
  3. <item android:drawable= "@drawable/home" />
  4. </selector>

style

  1. <style name = "radiobutton_style" >
  2. <item name = "android:layout_width" >0dp</item>
  3. <item name = "android:padding" >3dp</item>
  4. <item name = "android:layout_height" >match_parent</item>
  5. <item name = "android:layout_weight" >1</item>
  6. <item name = "android:button" >@ null </item><! --Remove the default dot of RadioButton-->  
  7. <item name = "android:gravity" >center</item>
  8. <item name = "android:textSize" >12sp</item>
  9. </style>

Code

The initialization code is not recorded, it is all findViewById, and the implementation process is nothing more than monitoring the RadioButton:

  1. mRadioGroup.setOnCheckedChangeListener(this);
  2.  
  3.  
  4. @Override
  5. public void onCheckedChanged(RadioGroup group , int checkId) {
  6. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  7. switch (checkId) {
  8. case R.id.rb_home:
  9. if (mHomeFragment == null ) {
  10. mHomeFragment = HomeFragment.newInstance(getString(R.string.item_home));
  11. }
  12. transaction .replace ( R.id.sub_content , mHomeFragment);
  13. break;
  14. case R.id.rb_location:
  15. if (mLocationFragment == null ) {
  16. mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location));
  17. }
  18. transaction .replace ( R.id.sub_content , mLocationFragment);
  19. break;
  20. case R.id.rb_like:
  21. if (mLikeFragment == null ) {
  22. mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like));
  23. }
  24. transaction .replace ( R.id.sub_content , mLikeFragment);
  25. break;
  26. case R.id.rb_me:
  27. if (mPersonFragment == null ) {
  28. mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person));
  29. }
  30. transaction .replace ( R.id.sub_content , mPersonFragment);
  31. break;
  32. }
  33. setTabState(); //Set state
  34. transaction . commit ();
  35. }

Status settings

  1. private void setTabState() {
  2. setHomeState();
  3. setLocationState();
  4. setLikeState();
  5. setMeState();
  6.  
  7. }
  8.  
  9. /**
  10. * set tab home state
  11. */
  12. private void setHomeState() {
  13. if (mRadioHome.isChecked()) {
  14. mRadioHome.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
  15. } else {
  16. mRadioHome.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
  17. }
  18. }
  19.  
  20. private void setLocationState() {
  21. if (mRadioLocation.isChecked()) {
  22. mRadioLocation.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
  23. } else {
  24. mRadioLocation.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
  25. }
  26. }
  27.  
  28. private void setLikeState() {
  29. if (mRadioLike.isChecked()) {
  30. mRadioLike.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
  31. } else {
  32. mRadioLike.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
  33. }
  34. }
  35.  
  36. private void setMeState() {
  37. if (mRadioMe.isChecked()) {
  38. mRadioMe.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
  39. } else {
  40. mRadioMe.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
  41. }
  42. }

It should be noted here that setDefaultFragment(); I wrote in onCreateVew and it did not take effect. Here I wrote it in the onStart() method.

  1. @Override
  2. public void onStart() {
  3. setDefaultFragment(); //Write in onCreateView, it will not take effect when the page runs to other Fragments and then comes back
  4. super.onStart();
  5. }

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 (I) BottomNavigationBar

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

Recommend

Baidu Jimuyu promotion landing page construction process

How to build a Jimuyu page? How to make the Jimuy...

How to make a WeChat red envelope cover? WeChat Red Packet Cover Making Tutorial

How to make a WeChat red envelope cover? Tutorial...

An effective method for building an APP user system!

We must be clear that building a user growth syst...

Will base station signal radiation affect health?

[[344482]] This article is reprinted from the WeC...

Runtime things (message mechanism)

[[163324]] 1. About runtime I have used runtime t...

Frequently asked questions about paid promotion in Huawei AppGallery!

Paid Marketing FAQ 1. FAQ about paid promotion Q1...

The ultimate way to keep warm is to eat?! What foods can keep the body warmer?

Winter is coming, and many people are concerned a...

Is the TV box the most power-consuming appliance in the home?

Do you know which appliance in your home consumes ...