MainActivity – onCreate()
... SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), numOfPages); ViewPager viewPager = (ViewPager) findViewById(R.id.container_view_pager); viewPager.setAdapter(mSectionsPagerAdapter); viewPager.setCurrentItem(1000 * numOfPages); ...
FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter { private int numOfPages; public SectionsPagerAdapter(FragmentManager fm, int numOfPages) { super(fm); this.numOfPages = numOfPages; } @Override public Fragment getItem(int position) { while (position >= numOfPages) position -= numOfPages; return YourFragment.newInstance(position + 1); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public CharSequence getPageTitle(int position) { return null; } }
YourFragment:
public class YourFragment extends Fragment { private static final String ARG_SECTION_NUMBER = "section_number"; private int sectionNum; public static YourFragment newInstance(int sectionNumber) { YourFragment fragment = new YourFragment (); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_your, container, false); sectionNum = getArguments().getInt(ARG_SECTION_NUMBER) - 1; return rootView; } }