布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baiw.testdeom.View.activity.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/Main_Viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/Main_RadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="漫画"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_02"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="社区"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_03"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="会员"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_04"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="我的"
android:textSize="25sp" />
</RadioGroup>
</LinearLayout>
private void initView() {//找控件
rg = (RadioGroup) findViewById(R.id.Main_RadioGroup);
rg.setOnCheckedChangeListener(this);
rb_1 = (RadioButton) findViewById(R.id.rb_01);
rb_2 = (RadioButton) findViewById(R.id.rb_02);
rb_3 = (RadioButton) findViewById(R.id.rb_03);
rb_4 = (RadioButton) findViewById(R.id.rb_04);
viewPager = (ViewPager) findViewById(R.id.Main_pager);
//初始化适配器
adapter = new MainPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
//viewpager的监听
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {//pager滑动的时候,根据角标radiobutton被选中
switch (position) {
case 0:
rb_1.setChecked(true);
break;
case 1:
rb_2.setChecked(true);
break;
case 2:
rb_3.setChecked(true);
break;
case 3:
rb_4.setChecked(true);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
初始化Fragment并添加到集合中
private void initEventes() {
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
Fragment3 fragment3 = new Fragment3();
Fragment4 fragment4 = new Fragment4();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
fragments.add(fragment4);
}
把集合传到适配器中
private void initGetDate() {
adapter.getDate(fragments);
adapter.notifyDataSetChanged();
}
//点击rg可以跳转
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
int current = 0; //初始值为0
switch (checkedId) {
case R.id.rb_01:
current = 0;//第一个pager界面是0
break;
case R.id.rb_02:
current = 1;
break;
case R.id.rb_03:
current = 2;
break;
case R.id.rb_04:
current = 3;
break;
}
if (viewPager.getCurrentItem() != current) {
//vp.setCurrentItem(current);
viewPager.setCurrentItem(current, false);
}
}