Maver1ck

Untitled

Jul 30th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. public class Main extends FragmentActivity implements ActionBar.TabListener {
  2.  
  3.     /**
  4.      * The {@link android.support.v4.view.PagerAdapter} that will provide
  5.      * fragments for each of the sections. We use a
  6.      * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
  7.      * will keep every loaded fragment in memory. If this becomes too memory
  8.      * intensive, it may be best to switch to a
  9.      * {@link android.support.v4.app.FragmentStatePagerAdapter}.
  10.      */
  11.     SectionsPagerAdapter mSectionsPagerAdapter;
  12.  
  13.     /**
  14.      * The {@link ViewPager} that will host the section contents.
  15.      */
  16.     ViewPager mViewPager;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.main);
  22.  
  23.         // Set up the action bar.
  24.         final ActionBar actionBar = getActionBar();
  25.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  26.  
  27.         // Create the adapter that will return a fragment for each of the three
  28.         // primary sections of the app.
  29.         mSectionsPagerAdapter = new SectionsPagerAdapter(
  30.                 getSupportFragmentManager());
  31.  
  32.         // Set up the ViewPager with the sections adapter.
  33.         mViewPager = (ViewPager) findViewById(R.id.pager);
  34.         mViewPager.setAdapter(mSectionsPagerAdapter);
  35.  
  36.         // When swiping between different sections, select the corresponding
  37.         // tab. We can also use ActionBar.Tab#select() to do this if we have
  38.         // a reference to the Tab.
  39.         mViewPager
  40.                 .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
  41.                     @Override
  42.                     public void onPageSelected(int position) {
  43.                         actionBar.setSelectedNavigationItem(position);
  44.                     }
  45.                 });
  46.  
  47.         // For each of the sections in the app, add a tab to the action bar.
  48.         for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
  49.             // Create a tab with text corresponding to the page title defined by
  50.             // the adapter. Also specify this Activity object, which implements
  51.             // the TabListener interface, as the callback (listener) for when
  52.             // this tab is selected.
  53.             actionBar.addTab(actionBar.newTab()
  54.                     .setText(mSectionsPagerAdapter.getPageTitle(i))
  55.                     .setTabListener(this));
  56.         }
  57.     }
  58.  
  59.     @Override
  60.     public boolean onCreateOptionsMenu(Menu menu) {
  61.         // Inflate the menu; this adds items to the action bar if it is present.
  62.         getMenuInflater().inflate(R.menu.main, menu);
  63.         return true;
  64.     }
  65.  
  66.     @Override
  67.     public void onTabSelected(ActionBar.Tab tab,
  68.             FragmentTransaction fragmentTransaction) {
  69.         // When the given tab is selected, switch to the corresponding page in
  70.         // the ViewPager.
  71.         mViewPager.setCurrentItem(tab.getPosition());
  72.     }
  73.  
  74.     @Override
  75.     public void onTabUnselected(ActionBar.Tab tab,
  76.             FragmentTransaction fragmentTransaction) {
  77.     }
  78.  
  79.     @Override
  80.     public void onTabReselected(ActionBar.Tab tab,
  81.             FragmentTransaction fragmentTransaction) {
  82.     }
  83.  
  84.     /**
  85.      * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  86.      * one of the sections/tabs/pages.
  87.      */
  88.     public class SectionsPagerAdapter extends FragmentPagerAdapter {
  89.  
  90.         public SectionsPagerAdapter(FragmentManager fm) {
  91.             super(fm);
  92.         }
  93.  
  94.         @Override
  95.         public Fragment getItem(int position) {
  96.             // getItem is called to instantiate the fragment for the given page.
  97.             // Return a DummySectionFragment (defined as a static inner class
  98.             // below) with the page number as its lone argument.
  99.             Fragment fragment = new DummySectionFragment();
  100.             Bundle args = new Bundle();
  101.             args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
  102.             fragment.setArguments(args);
  103.             return fragment;
  104.         }
  105.  
  106.         @Override
  107.         public int getCount() {
  108.             // Show 3 total pages.
  109.             return 3;
  110.         }
  111.  
  112.         @Override
  113.         public CharSequence getPageTitle(int position) {
  114.             Locale l = Locale.getDefault();
  115.             switch (position) {
  116.             case 0:
  117.                 return getString(R.string.title_section1).toUpperCase(l);
  118.             case 1:
  119.                 return getString(R.string.title_section2).toUpperCase(l);
  120.             case 2:
  121.                 return getString(R.string.title_section3).toUpperCase(l);
  122.             }
  123.             return null;
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * A dummy fragment representing a section of the app, but that simply
  129.      * displays dummy text.
  130.      */
  131.     public static class DummySectionFragment extends Fragment {
  132.         /**
  133.          * The fragment argument representing the section number for this
  134.          * fragment.
  135.          */
  136.         public static final String ARG_SECTION_NUMBER = "section_number";
  137.  
  138.         public DummySectionFragment() {
  139.         }
  140.  
  141.         @Override
  142.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  143.                 Bundle savedInstanceState) {
  144.             View rootView = inflater.inflate(R.layout.fragment_main_dummy,
  145.                     container, false);
  146.             TextView dummyTextView = (TextView) rootView
  147.                     .findViewById(R.id.section_label);
  148.             dummyTextView.setText(Integer.toString(getArguments().getInt(
  149.                     ARG_SECTION_NUMBER)));
  150.             return rootView;
  151.         }
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment