Andoird development - bottom navigation bar with shards and radiogroups

Set the navigation bar at the bottom. Click this item to highlight it. According to the way of small white dots in the V...

Set the navigation bar at the bottom. Click this item to highlight it. According to the way of small white dots in the ViewPager, create several selector s. Because RadioGroup is used, you need to judge whether to click through the Android state checked property. Here I just use text as an example, because I don't have the right picture.
Create a new background.xml in the drawable folder. When the enabled property is true, set the color to grass green, and when the property is false, set it to black.

filename:background.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="@color/grassgreen"/> <item android:state_checked="false" android:color="@color/black"/> </selector>

Then write the activity main.xml layout file, which consists of two parts, one is the main interface, the other is a fragment, the other is a RadioGroup control. Since there are two identical controls, you can write the property as a style and directly set the style property of the control.

filename:res/styles <style name="BottomNavItemStyle"> <item name="android:layout_weight">1</item> <item name="android:layout_height">match_parent</item> <item name="android:background">@color/palegoldenrod</item> <item name="android:textColor">@drawable/background</item> <item name="android:button">@null</item> <item name="android:gravity">center</item> </style>

RadioButton also wants to have an attribute: drawabletop can be used to set the attributes of pictures. It also uses selector to cry, instead of using RelativeLayout, instead of using vertical LinearLayout.

filename:activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.k.androidpractie.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/MainFragment"></FrameLayout> <RadioGroup android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="50dp" android:id="@+id/BottonNav"> <RadioButton android:id="@+id/Page_1" style="@style/BottomNavItemStyle" android:text="First page"/> <RadioButton android:id="@+id/Page_2" style="@style/BottomNavItemStyle" android:text="Second pages"/> </RadioGroup> </LinearLayout>

Then create 2 fragments and change the text

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_gravity="center" android:text="First"/> </LinearLayout>

Create two new fragment classes and load the fragments

public class MainFrag_1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.main_frag_1,container,false); return view; } }

Finally, it is written in MainActivity.java. In fact, it's useless to get the RadioButton instance. Get the RadioGroup instance and set the monitor. Then, it can judge which RadioButton is by checkedd.
Then it seems that FragmentTransaction has to be retrieved after a commit(), otherwise it will report an error and commit.
Fragment instances can be added by the add() method or replaced by replace().
For a method, first hide all fragment instances, if any, by calling hide() method. If the fragment instance is a, you can first determine whether a is null, that is, whether it exists. If it does not exist, new one, and show() if it exists. Because it is possible that the original data needs to be saved in the interface, if new is used every time, the data may be lost.

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { FrameLayout MainFragmentFrameLayout; RadioButton Page_1_Button,Page_2_Button; RadioGroup BottomNavRadioGroup; MainFrag_1 mainFrag_1; MainFrag_2 mainFrag_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MainFragmentFrameLayout=findViewById(R.id.MainFragment); BottomNavRadioGroup=findViewById(R.id.BottonNav); Page_1_Button=findViewById(R.id.Page_1); Page_2_Button=findViewById(R.id.Page_2); Page_1_Button.setChecked(true); //fragmentTransaction.replace(R.id.MainFragment,new MainFrag_1()); BottomNavRadioGroup.setOnCheckedChangeListener(this) ; } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentManager fragmentManager=getSupportFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); hideFragment(fragmentTransaction); switch(checkedId){ case R.id.Page_1: if (mainFrag_1==null){ mainFrag_1=new MainFrag_1(); fragmentTransaction.add(R.id.MainFragment,mainFrag_1); }else{ fragmentTransaction.show(mainFrag_1); } break; case R.id.Page_2: if (mainFrag_2==null){ mainFrag_2=new MainFrag_2(); fragmentTransaction.add(R.id.MainFragment,mainFrag_2); }else{ fragmentTransaction.show(mainFrag_2); } break; } fragmentTransaction.commit(); } public void hideFragment(FragmentTransaction fragmentTransaction){ if (mainFrag_1!=null){ fragmentTransaction.hide(mainFrag_1); } if (mainFrag_2!=null){ fragmentTransaction.hide(mainFrag_2); } } }

Then run the program to see the effect

BluePROT Published 21 original articles, won praise 7, visited 7557 Private letter follow

5 February 2020, 04:43 | Views: 5379

Add new comment

For adding a comment, please log in
or create account

0 comments