新建adapter继承FragmentPagerAdapter
public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
private FragmentManager mFragmentManager;
private List<Fragment> mList;
public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.mList = list;
}
@Override
public Fragment getItem(int position) {
return mList.get(position);
}
@Override
public int getCount() {
return mList.size();
}
}
在activity中的使用
private ViewPager viewPager;
private TabFragmentPagerAdapter tabFragmentPagerAdapter;
private List<Fragment> fragments;
fragments = new ArrayList<>();
fragments.add(new MainFragment());
fragments.add(new ListFragment());
fragments.add(new ThreeFragment());
fragments.add(new StudyFragment());
tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(tabFragmentPagerAdapter);