@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//数组
final ArrayList<Fragment> list=new ArrayList<>();
list.add(new Fragment_home());
list.add(new Fragment_quanzi());
list.add(new Fragment_gwc());
list.add(new Fragment_dd());
list.add(new Fragment_mine());
//设置适配器
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
//默认第一个
group.check(group.getChildAt(0).getId());
//按钮滑动 页面跟着变
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio1:
pager.setCurrentItem(0);
break;
case R.id.radio2:
pager.setCurrentItem(1);
break;
case R.id.radio3:
pager.setCurrentItem(2);
break;
case R.id.radio4:
pager.setCurrentItem(3);
break;
case R.id.radio5:
pager.setCurrentItem(4);
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) {
}
});
}
}