public class MainActivity extends FragmentActivity implements OnCheckedChangeListener{
private ViewPager vpager;
private RadioGroup group;
private RadioButton but1;
private RadioButton but2;
private RadioButton but3;
private List<Fragment> asList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vpager = (ViewPager) findViewById(R.id.Vpager);
group = (RadioGroup) findViewById(R.id.group);
but1 = (RadioButton) findViewById(R.id.radio_but1);
but2 = (RadioButton) findViewById(R.id.radio_but2);
but3 = (RadioButton) findViewById(R.id.radio_but3);
group.setOnCheckedChangeListener(this);
asList = Arrays.asList(new Fragment1(), new Fragment2(), new Fragment3());
initLintener();
}
private void initLintener() {
vpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return asList != null ? asList.size() : 0;
}
@Override
public Fragment getItem(int arg0) {
return asList.get(arg0);
}
});
vpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
but1.setChecked(true);
break;
case 1:
but2.setChecked(true);
break;
case 2:
but3.setChecked(true);
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int count = 0;
switch (checkedId) {
case R.id.radio_but1:
count = 0;
break;
case R.id.radio_but2:
count = 1;
break;
case R.id.radio_but3:
count = 2;
break;
}
if (vpager.getCurrentItem() != count) {
vpager.setCurrentItem(count, false);
}
}
}
private ViewPager vpager;
private RadioGroup group;
private RadioButton but1;
private RadioButton but2;
private RadioButton but3;
private List<Fragment> asList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vpager = (ViewPager) findViewById(R.id.Vpager);
group = (RadioGroup) findViewById(R.id.group);
but1 = (RadioButton) findViewById(R.id.radio_but1);
but2 = (RadioButton) findViewById(R.id.radio_but2);
but3 = (RadioButton) findViewById(R.id.radio_but3);
group.setOnCheckedChangeListener(this);
asList = Arrays.asList(new Fragment1(), new Fragment2(), new Fragment3());
initLintener();
}
private void initLintener() {
vpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return asList != null ? asList.size() : 0;
}
@Override
public Fragment getItem(int arg0) {
return asList.get(arg0);
}
});
vpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
but1.setChecked(true);
break;
case 1:
but2.setChecked(true);
break;
case 2:
but3.setChecked(true);
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int count = 0;
switch (checkedId) {
case R.id.radio_but1:
count = 0;
break;
case R.id.radio_but2:
count = 1;
break;
case R.id.radio_but3:
count = 2;
break;
}
if (vpager.getCurrentItem() != count) {
vpager.setCurrentItem(count, false);
}
}
}