public
Created

  • Download Gist
FragmentPagerAdapter.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
public class FragmentPagerActivity extends FragmentActivity {
 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.blank_main);
//Setting the adapter
ViewPager mViewPager = (ViewPager) super.findViewById(R.id.mysixpanelpager);
mViewPager.setAdapter(initialisePaging());
mViewPager.setOffscreenPageLimit(2);
mViewPager.setCurrentItem(0);
}
 
private PagerAdapter initialisePaging() {
// Fragments to initialise
MainActivity mMainActivityFragment = new MainActivity();
Bundle args = new Bundle();
args.putInt("pagerId", R.id.mysixpanelpager);
mMainActivityFragment.setArguments(args);
OneMain mOneMainFragment = new OneMain();
TwoMain mTwoMainFragment = new TwoMain();
ThreeMain mThreeMainFragment = new ThreeMain();
FourMain mFourMainFragment = new FourMain();
FiveMain mFiveMainFragment = new FiveMain();
 
// Process of initialisation
PagerAdapter myFPadapter = new PagerAdapter(getSupportFragmentManager());
myFPadapter.addFragment(mMainActivityFragment);
myFPadapter.addFragment(mOneMainFragment);
myFPadapter.addFragment(mTwoMainFragment);
myFPadapter.addFragment(mThreeMainFragment);
myFPadapter.addFragment(mFourMainFragment);
myFPadapter.addFragment(mFiveMainFragment);
return myFPadapter;
}
 
private static class PagerAdapter extends FragmentPagerAdapter {
 
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
 
public PagerAdapter(FragmentManager fm) {
super(fm);
}
 
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
 
@Override
public int getCount() {
return mFragments.size();
}
 
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.