This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 21st, 2012  |  syntax: Java  |  size: 2.24 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.android.history;
  2.  
  3. import java.util.ArrayList;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.app.FragmentActivity;
  7. import android.support.v4.app.FragmentManager;
  8. import android.support.v4.app.FragmentPagerAdapter;
  9. import android.support.v4.view.ViewPager;
  10.  
  11. public class FragmentPagerActivity extends FragmentActivity {
  12.        
  13.         public void onCreate(Bundle savedInstanceState){
  14.                 super.onCreate(savedInstanceState);
  15.                 super.setContentView(R.layout.blank_main);
  16.        
  17.         //Setting the adapter
  18.     ViewPager mViewPager = (ViewPager) super.findViewById(R.id.mysixpanelpager);
  19.     mViewPager.setAdapter(initialisePaging());
  20.     mViewPager.setOffscreenPageLimit(2);
  21.     mViewPager.setCurrentItem(0);
  22.        
  23.         }
  24.                
  25.                 private PagerAdapter initialisePaging() {
  26.                        
  27.                         //Fragments to initialise
  28.                     MainActivity mMainActivityFragment = new MainActivity();
  29.                     OneMain mOneMainFragment = new OneMain();
  30.                     TwoMain mTwoMainFragment = new TwoMain();
  31.                     ThreeMain mThreeMainFragment = new ThreeMain();
  32.                     FourMain mFourMainFragment = new FourMain();
  33.                     FiveMain mFiveMainFragment = new FiveMain();
  34.                    
  35.                     //Process of initialisation
  36.                     PagerAdapter myFPadapter = new PagerAdapter(getSupportFragmentManager());
  37.                     myFPadapter.addFragment(mMainActivityFragment);
  38.                     myFPadapter.addFragment(mOneMainFragment);
  39.                     myFPadapter.addFragment(mTwoMainFragment);
  40.                     myFPadapter.addFragment(mThreeMainFragment);
  41.                     myFPadapter.addFragment(mFourMainFragment);
  42.                     myFPadapter.addFragment(mFiveMainFragment);
  43.                         return myFPadapter;
  44.                
  45.         }
  46.        
  47.         private static class PagerAdapter extends FragmentPagerAdapter {  
  48.  
  49.                 private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
  50.                
  51.         public PagerAdapter(FragmentManager fm) {  
  52.              super(fm);  
  53.         }  
  54.        
  55.         public void addFragment(Fragment fragment) {
  56.             mFragments.add(fragment);
  57.             notifyDataSetChanged();
  58.         }
  59.        
  60.         @Override
  61.         public int getCount() {
  62.             return mFragments.size();
  63.         }
  64.  
  65.         @Override
  66.         public Fragment getItem(int position) {
  67.             return mFragments.get(position);
  68.         }
  69.  
  70.    }  
  71.  
  72. }
clone this paste RAW Paste Data