我的应用程序使用带有 5 个滑动选项卡的 ViewPager。在大型设备和横向运行应用程序时,我想在我的 ViewPager 上扩展 2 个片段或 2 个标签页,在较小的设备或纵向视图上运行时,我想在我的 ViewPager 上扩展 1 个片段或 1 个标签页。此外,它应该仍然能够像我在 ViewPager 上显示单个片段时通常那样滑动浏览所有选项卡。
我在扩展片段的 CourseFragment 类中初始化并扩展了 ViewPager:
ViewPager mViewPager = (ViewPager) getView().findViewById(
R.id.pagerYear);
InfoTechPageAdapter infoTechPageAdapter = new InfoTechPageAdapter(
getChildFragmentManager());
mViewPager.setOffscreenPageLimit(5);
mViewPager.setAdapter(infoTechPageAdapter);
我的 InfoTechPageAdapter 扩展了 FragmentStatePagerAdapter:
package com.usjr.sss.adapter;
import com.usjr.sss.fragment.InfoTechFirstYearFragment;
import com.usjr.sss.fragment.InfoTechFourthYearFragment;
import com.usjr.sss.fragment.InfoTechSecondYearFragment;
import com.usjr.sss.fragment.InfoTechThirdYearFragment;
import com.usjr.sss.fragment.SubmitSubjectFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class InfoTechPageAdapter extends FragmentStatePagerAdapter {
public InfoTechPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
Fragment mFragment;
switch (pos) {
case 0:
mFragment = new InfoTechFirstYearFragment();
mFragment.setArguments(null);
return mFragment;
case 1:
mFragment = new InfoTechSecondYearFragment();
mFragment.setArguments(null);
return mFragment;
case 2:
mFragment = new InfoTechThirdYearFragment();
mFragment.setArguments(null);
return mFragment;
case 3:
mFragment = new InfoTechFourthYearFragment();
mFragment.setArguments(null);
return mFragment;
case 4:
mFragment = new SubmitSubjectFragment();
mFragment.setArguments(null);
return mFragment;
default:
return null;
}
}// end getItem
@Override
public int getCount() {
return 5;
}// end getCount
@Override
public CharSequence getPageTitle(int pos) {
// Locale l = Locale.getDefault();
switch (pos) {
case 0:
return "FIRST YEAR";
case 1:
return "SECOND YEAR";
case 2:
return "THIRD YEAR";
case 3:
return "FOURTH YEAR";
case 4:
return "SUBMIT";
}
return null;
}
}