import android.graphics.Color; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.ListFragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ViewPager vp; private RadioGroup rg; private RadioButton rb1,rb2,rb3,rb4; private List<Fragment> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = findViewById(R.id.vp); rg = findViewById(R.id.rg); rb1 = findViewById(R.id.rb1); rb2 = findViewById(R.id.rb2); rb3 = findViewById(R.id.rb3); rb4 = findViewById(R.id.rb4); list = new ArrayList<>(); Frag1 f1=new Frag1(); Frag2 f2=new Frag2(); Frag3 f3=new Frag3(); Frag4 f4=new Frag4(); list.add(f1); list.add(f2); list.add(f3); list.add(f4); vp.setAdapter(new MyFrag(getSupportFragmentManager())); vp.setCurrentItem(0); rb1.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.rb1: vp.setCurrentItem(0); rb1.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); break; case R.id.rb2: vp.setCurrentItem(1); rb2.setTextColor(Color.RED); rb1.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); break; case R.id.rb3: vp.setCurrentItem(2); rb3.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb1.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); break; case R.id.rb4: vp.setCurrentItem(3); rb4.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb1.setTextColor(Color.BLACK); break; default: break; } } }); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub switch (arg0) { case 0: rb1.setChecked(true); rb1.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); break; case 1: rb2.setChecked(true); rb2.setTextColor(Color.RED); rb1.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); break; case 2: rb3.setChecked(true); rb3.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb1.setTextColor(Color.BLACK); break; case R.id.rb4: rb4.setChecked(true); rb4.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb1.setTextColor(Color.BLACK); break; default: break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } class MyFrag extends FragmentPagerAdapter{ public MyFrag(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } } }