The core code
public class FirstFragment extends Fragment { private TextView textView; Nullable @override // similar to setContentView() in the Activity; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.first_fragment,container,false); return view; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); textView=view.findViewById(R.id.rb1); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_fragment); } void setContentView(int first_fragment) { } }Copy the code
First you need to write out the layout of the top or bottom navigation bar
<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout 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"> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent"> <RadioButton android:id="@+id/rb1" android:layout_width="146dp" android:layout_height="40dp" android:text="Company Setup" android:textColor="@color/black" android:textSize="10sp" android:gravity="center" android:background="@drawable/star_button_select" /> <RadioButton android:id="@+id/rb2" android:layout_width="146dp" android:layout_height="40dp" android:text="Business Rules Setup" android:textColor="@color/black" android:textSize="10sp" android:gravity="center" android:background="@drawable/star_button_select" /> <RadioButton android:id="@+id/rb3" android:layout_width="146dp" android:layout_height="40dp" android:text="Master Setup" android:textColor="@color/black" android:textSize="10sp" android:gravity="center" android:background="@drawable/star_button_select" /> </RadioGroup> <androidx.viewpager.widget.ViewPager android:id="@+id/vpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="40dp"/> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code
Remember: A RadioButton needs to be inside a RadioGroup for listening to work
Then write the activity content
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener { private RadioGroup group; private RadioButton rb1; private RadioButton rb2; private RadioButton rb3; private ViewPager vpager; private MyFragmentPagerAdapter mAdapter; Public static final int PAGE_ONE = 0; public static final int PAGE_TWO = 1; public static final int PAGE_THREE = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setup_list); mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); initViews(); rb1.setChecked(true); } private void initViews() { group = findViewById(R.id.group); rb1 = findViewById(R.id.rb1); rb2 = findViewById(R.id.rb2); rb3 = findViewById(R.id.rb3); group.setOnCheckedChangeListener(this); vpager = findViewById(R.id.vpager); vpager.setAdapter(mAdapter); vpager.setCurrentItem(0); vpager.addOnPageChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb1: vpager.setCurrentItem(PAGE_ONE); break; case R.id.rb2: vpager.setCurrentItem(PAGE_TWO); break; case R.id.rb3: vpager.setCurrentItem(PAGE_THREE); break; } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { if (state == 2) { switch (vpager.getCurrentItem()) { case PAGE_ONE: rb1.setChecked(true); break; case PAGE_TWO: rb2.setChecked(true); break; case PAGE_THREE: rb3.setChecked(true); break; } } } @Override public void onPointerCaptureChanged(boolean hasCapture) { } }Copy the code
The second is the content of each fragment, that is, the core code I gave above, I will not repeat here
Then there is the layout code for each fragment
<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv1" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:gravity="center" Android :textColor="@color/black" Android :textSize="25sp" Android :text=" this is the first page" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code
I’m just going to write one here, and I’m going to lay out the rest as I see fit
It’s been written since then.