public class MainActivity extends FragmentActivity {
private ViewPager pager;
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = findViewById(R.id.pager);
group = findViewById(R.id.group);
ArrayList<Fragment> list=new ArrayList<>();
list.add(new Frag1());
list.add(new Frag2());
list.add(new Frag3());
list.add(new Frag4());
//默认选中
group.check(group.getChildAt(0).getId());
pager.setAdapter(new PagerAdapter(getSupportFragmentManager(),list));
//单选按钮点击
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.button1:
pager.setCurrentItem(0);
break;
case R.id.button2:
pager.setCurrentItem(1);
break;
case R.id.button3:
pager.setCurrentItem(2);
break;
case R.id.button4:
pager.setCurrentItem(3);
break;
}
}
});
//页面滑动切换时 单选按钮跟着切换
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
group.check(group.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
}
微信页面 动态fragment
最新推荐文章于 2023-06-26 12:54:48 发布