布局文件
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:id="@+id/vp">
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/rg">
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_weight="1"
android:button="@null"
android:text="第一页"
android:textSize="25sp"
android:gravity="center"
android:id="@+id/rb1"/>
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_weight="1"
android:text="首页"
android:button="@null"
android:textSize="25sp"
android:gravity="center"
android:id="@+id/rb2"/>
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_weight="1"
android:text="第二页"
android:button="@null"
android:textSize="25sp"
android:gravity="center"
android:id="@+id/rb3"/>
在main实现
public class MainActivityextends AppCompatActivity {
private ViewPagervp;
private RadioGrouprg;
private ArrayListlist=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
rg = (RadioGroup) findViewById(R.id.rg);
list .add(new Fragm1());
list .add(new Fragm2());
list .add(new Fragm3());
//创建适配器
Myadapter myadapter =new Myadapter(getSupportFragmentManager());
vp.setAdapter(myadapter);
//过时方法
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i,float v,int i1) {
}
@Override
public void onPageSelected(int i) {
MainActivity.this.rg.check(MainActivity.this.rg.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
//
this.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup,int j) {
switch (j){
case R.id.rb1:
vp.setCurrentItem(0);
break;
case R.id.rb2:
vp.setCurrentItem(1);
break;
case R.id.rb3:
vp.setCurrentItem(2);
break;
}
}
});
}
//适配器
class Myadapterextends FragmentPagerAdapter {
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
}
//fragment 布局
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一页"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_marginTop="266dp"/>
//fragment class
public class Fragm1extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragm1,null);
return view;
}