private ArrayList<Fragment> fragmentList = new ArrayList();
private String[] mTitle = new String[]{"全部直播", "正在直播"};
fragmentList.add(LiveAllFragment.newInstance(LiveConstant.ALLLIVE));
fragmentList.add(LiveAllFragment.newInstance(LiveConstant.PLAYINGLIVE));
BaseFragmentPagerAdapter baseFragmentPagerAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), mTitle, fragmentList);
vp.setAdapter(baseFragmentPagerAdapter);
vp.setOffscreenPageLimit(mTitle.length - 1);//预加载fragment
tab.setupWithViewPager(vp);
public class BaseFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] mTitles;
private ArrayList<Fragment> fragmentList;
public BaseFragmentPagerAdapter(FragmentManager fm, String[] mTitles, ArrayList<Fragment> fragmentList) {
super(fm);
this.mTitles = mTitles;
this.fragmentList = fragmentList;
}
public BaseFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
public CharSequence getPageTitle(int position) {
return mTitles == null || mTitles.length == 0 ? "" : mTitles[position];
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
}