You can create the same fragment class for each page of your ViewPager by passing the ViewPager position to control the display. Something like that:
public class MyFragment extends Fragment { private int mIndex; public MyFragment(int index) { mIndex = index; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { switch(mIndex){ case 0:
then you have the FragmentPagerAdapter:
public static class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { return new MyFragment(position); } }
This way you can reuse most of your code by changing only what you need in the switch / case statement.
source share