布局111
public class MainActivity extends FragmentActivity { private ViewPager vp; private List<Fragment> list; private Fragment01 fragment01; private Fragment02 fragment02; private Fragment03 fragment03; private Fragment04 fragment04; private Fragment05 fragment05; private RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = findViewById(R.id.vp); rg = findViewById(R.id.rg); list = new ArrayList<Fragment>(); fragment01 = new Fragment01(); fragment02 = new Fragment02(); fragment03 = new Fragment03(); fragment04 = new Fragment04(); fragment05 = new Fragment05(); list.add(fragment01); list.add(fragment02); list.add(fragment03); list.add(fragment04); list.add(fragment05); vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.btn1: vp.setCurrentItem(0); break; case R.id.btn2: vp.setCurrentItem(1); break; case R.id.btn3: vp.setCurrentItem(2); break; case R.id.btn4: vp.setCurrentItem(3); break; case R.id.btn5: vp.setCurrentItem(4); break; } } }); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position){ case 0: rg.check(R.id.btn1); break; case 1: rg.check(R.id.btn2); break; case 2: rg.check(R.id.btn3); break; case 3: rg.check(R.id.btn4); break; case 4: rg.check(R.id.btn5); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } }
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" android:layout_weight="1" > </android.support.v4.view.ViewPager> <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rg" android:layout_weight="9" android:orientation="horizontal"> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/btn1" android:layout_weight="1" android:gravity="center" android:text="首页" android:button="@null"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/btn2" android:layout_weight="1" android:gravity="center" android:text="想法" android:button="@null"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/btn3" android:layout_weight="1" android:gravity="center" android:text="市场" android:button="@null"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/btn4" android:layout_weight="1" android:gravity="center" android:text="通知" android:button="@null"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/btn5" android:layout_weight="1" android:gravity="center" android:text="更多" android:button="@null"/> </RadioGroup> </LinearLayout>