public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
private ArrayList<Fragment> arrayList;
private FragmentManager manager;
private RadioGroup radioGroup;
private RadioButton[] radioButtons;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.main_vp);
radioGroup= (RadioGroup) findViewById(R.id.main_rg);
manager=getSupportFragmentManager();
arrayList=new ArrayList<>();
Fragment1 fragment1=new Fragment1();
Fragment2 fragment2=new Fragment2();
Fragment3 fragment3=new Fragment3();
Fragment4 fragment4=new Fragment4();
Fragment5 fragment5=new Fragment5();
arrayList.add(fragment1);
arrayList.add(fragment2);
arrayList.add(fragment3);
arrayList.add(fragment4);
arrayList.add(fragment5);
Main_Adapter adapter=new Main_Adapter(manager,arrayList);
viewPager.setAdapter(adapter);
radioButtons=new RadioButton[radioGroup.getChildCount()];
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i]= (RadioButton) radioGroup.getChildAt(i);
}
radioButtons[0].setChecked(true);
radioGroup.setOnCheckedChangeListener(this);
viewPager.setOnPageChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
for (int j = 0; j < radioButtons.length; j++) {
if (radioButtons[j].getId()==checkedId){
viewPager.setCurrentItem(j);
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
radioButtons[position].setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}