In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use RadioGroup and Fragment in Android to achieve the function of the bottom navigation bar", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use RadioGroup and Fragment in Android to achieve the function of the bottom navigation bar.
Take a look at the effect picture first.
Step 1:
Complete the creation of the main interface main.xml:
Reused styles in radioButton: extracted and written in style
Match_parent match_parent 5dp center @ drawable/rb_text_color 16sp normal
After clicking RadioButton, the text color of the navigation bar changes, which is declared in drawable.
The name is: the rb_text_color code is as follows:
The navigation bar icon has changed. Only one of them is written here, and the other three are basically the same:
After completing these basic steps, you need to write the layout of the Fragment
Write one of them and the other three are similar.
After that, create Fragment in the background code, and write one of them here: CartFragment
Package com.example.fragmentdemo;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.annotation.Nullable;import androidx.fragment.app.Fragment;public class CartFragment extends Fragment {private View view; private TextView tv_home @ Nullable @ Override public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {if (view==null) {view= inflater.inflate (R.layout.cartfragment.container.false);} return view;}}
Step 2: complete the switching function to fragment in MainActivity
Specific comments are given in the code.
Package com.example.fragmentdemo;import androidx.appcompat.app.AppCompatActivity;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentManager;import androidx.fragment.app.FragmentTransaction;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.RadioButton;import android.widget.RadioGroup;import java.time.LocalDate;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {private RadioButton rb_home,rb_discover,rb_cart,rb_user Private RadioGroup rg_group; private List fragments; private int position=0; private static final String TAG = "MainActivity"; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); rb_home=findViewById (R.id.rb_home); rb_discover=findViewById (R.id.rb_discover); rb_cart=findViewById (R.id.rb_cart) Rb_user=findViewById (R.id.rb_user); rg_group=findViewById (R.id.rg_group); / / first rb_home.setSelected (true); rg_group.setOnCheckedChangeListener (this); / / initialize fragment initFragment (); / / default layout, select the first defaultFragment () } private void defaultFragment () {FragmentManager fragmentManager = getSupportFragmentManager (); FragmentTransaction transaction = fragmentManager.beginTransaction (); transaction.replace (R.id.fragmentation layoutlayoutfragments.get (0)); transaction.commit ();} private void setSelected () {rb_home.setSelected (false); rb_discover.setSelected (false); rb_cart.setSelected (false) Rb_user.setSelected (false);} private void initFragment () {fragments = new ArrayList (); fragments.add (0meme new HomeFragment ()); fragments.add (1meme new DiscoverFragment ()); fragments.add (2je new CartFragment ()); fragments.add (3je new UserFragment ()) } @ Override public void onCheckedChanged (RadioGroup group, int I) {/ / get the fragment management class object FragmentManager fragmentManager = getSupportFragmentManager (); / / get the trigger of fragmentManager FragmentTransaction transaction = fragmentManager.beginTransaction (); switch (I) {case R.id.rb_home: position=0 / / call the replace method to replace fragment with the UI where fragment_layout id resides, or above the control / / this is the event of creating replace. If you want this event to be executed, you need to submit this event to the trigger / / use the commit () method transaction.replace (R.id.fragmentations layoutfragments.get (0)) / / set all navigation bars to the default color setSelected (); rb_home.setSelected (true); break; case R.id.rb_discover: position=1; transaction.replace (R.id.fragmentationlayoutfragments.get (1)) / / set all navigation bars to the default color setSelected (); rb_discover.setSelected (true); break; case R.id.rb_cart: position=2; transaction.replace (R.id.fragmentationlayoutfragments.get (2)) / / set all navigation bars to the default color setSelected (); rb_cart.setSelected (true); break; case R.id.rb_user: position=3; transaction.replace (R.id.fragmentationlayoutfragments.get (3)) / / set all navigation bars to the default color setSelected (); rb_user.setSelected (true); break;} / / event submission transaction.commit ();}}
This completes a simple bottom navigation bar function, which can only be switched by clicking on fragment, not by swiping left and right to switch fragment.
At this point, I believe you have a deeper understanding of "how to use RadioGroup and Fragment in Android to achieve the function of the bottom navigation bar". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.