代码逻辑很简单,大家看下代码就可以明白:
这是布局
<RelativeLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager_show"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/dp_12"
android:id="@+id/radio"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_btn1"
android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_60"
android:text="首页"
android:gravity="center"
android:textSize="16sp"
android:checked="true"
android:button="@null" />
<RadioButton
android:id="@+id/radio_btn2"
android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_60"
android:text="健康知识"
android:gravity="center"
android:textSize="16sp"
android:button="@null" />
<RadioButton
android:id="@+id/radio_btn3"
android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_60"
android:text="健管师"
android:gravity="center"
android:textSize="16sp"
android:button="@null" />
<RadioButton
android:id="@+id/radio_btn4"
android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_60"
android:text="商城"
android:gravity="center"
android:textSize="16sp"
android:button="@null" />
<RadioButton
android:id="@+id/radio_btn5"
android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_60"
android:text="我的"
android:gravity="center"
android:textSize="16sp"
android:button="@null" />
</RadioGroup>
</RelativeLayout>
</RelativeLayout>
代码逻辑
List<Fragment> list = new ArrayList<>();
ViewPager viewpagerShow;
RadioButton radioBtn1;
RadioButton radioBtn2;
RadioButton radioBtn3;
RadioGroup radio;
//添加fragment到集合中
list.add(new Fragment1());
list.add(new Fragment2());
list.add(new Fragment3());
list.add(new Fragment4());
list.add(new Fragment5());
//设置适配器
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), list);
viewpagerShow.setAdapter(myFragmentPagerAdapter);
//设置viewpager默认显示第一个
viewpagerShow.setCurrentItem(0);
//设置viewpager的监听
viewpagerShow.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
radio.check(radio.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
//设置radiogroup的监听
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_btn1:
viewpagerShow.setCurrentItem(0,false);
break;
case R.id.radio_btn2:
viewpagerShow.setCurrentItem(1,false);
break;
case R.id.radio_btn3:
viewpagerShow.setCurrentItem(2,false);
break;
case R.id.radio_btn4:
viewpagerShow.setCurrentItem(3,false);
break;
case R.id.radio_btn5:
viewpagerShow.setCurrentItem(4,false);
break;
}
}
});
}
初始化这部分我没有往上加,如果有帮助,欢迎多多关注。