1 package com.example.android.horizontalpaging;
2 
3 import android.app.ActionBar;
4 import android.app.FragmentTransaction;
5 import android.os.Bundle;
6 import android.support.v4.app.Fragment;
7 import android.support.v4.app.FragmentActivity;
8 import android.support.v4.app.FragmentManager;
9 import android.support.v4.app.FragmentPagerAdapter;
10 import android.support.v4.view.ViewPager;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.view.ViewGroup;
14 import android.widget.TextView;
15 
16 import java.util.Locale;
17 
18 public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
19 
20     /**
21      * The {@link android.support.v4.view.PagerAdapter} that will provide
22      * fragments for each of the sections. We use a
23      * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
24      * will keep every loaded fragment in memory. If this becomes too memory
25      * intensive, it may be best to switch to a
26      * {@link android.support.v4.app.FragmentStatePagerAdapter}.
27      */
28     SectionsPagerAdapter mSectionsPagerAdapter;
29 
30     /**
31      * The {@link ViewPager} that will host the section contents.
32      */
33     ViewPager mViewPager;
34 
35     /**
36      * Create the activity. Sets up an {@link android.app.ActionBar} with tabs, and then configures the
37      * {@link ViewPager} contained inside R.layout.activity_main.
38      *
39      * <p>A {@link SectionsPagerAdapter} will be instantiated to hold the different pages of
40      * fragments that are to be displayed. A
41      * {@link android.support.v4.view.ViewPager.SimpleOnPageChangeListener} will also be configured
42      * to receive callbacks when the user swipes between pages in the ViewPager.
43      *
44      * @param savedInstanceState
45      */
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         // Load the UI from res/layout/activity_main.xml
50         setContentView(R.layout.sample_main);
51 
52         // Set up the action bar. The navigation mode is set to NAVIGATION_MODE_TABS, which will
53         // cause the ActionBar to render a set of tabs. Note that these tabs are *not* rendered
54         // by the ViewPager; additional logic is lower in this file to synchronize the ViewPager
55         // state with the tab state. (See mViewPager.setOnPageChangeListener() and onTabSelected().)
56         // BEGIN_INCLUDE (set_navigation_mode)
57         final ActionBar actionBar = getActionBar();
58         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
59         // END_INCLUDE (set_navigation_mode)
60 
61         // BEGIN_INCLUDE (setup_view_pager)
62         // Create the adapter that will return a fragment for each of the three primary sections
63         // of the app.
64         mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
65 
66         // Set up the ViewPager with the sections adapter.
67         mViewPager = (ViewPager) findViewById(R.id.pager);
68         mViewPager.setAdapter(mSectionsPagerAdapter);
69         // END_INCLUDE (setup_view_pager)
70 
71         // When swiping between different sections, select the corresponding tab. We can also use
72         // ActionBar.Tab#select() to do this if we have a reference to the Tab.
73         // BEGIN_INCLUDE (page_change_listener)
74         mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
75             @Override
76             public void onPageSelected(int position) {
77                 actionBar.setSelectedNavigationItem(position);
78             }
79         });
80         // END_INCLUDE (page_change_listener)
81 
82         // BEGIN_INCLUDE (add_tabs)
83         // For each of the sections in the app, add a tab to the action bar.
84         for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
85             // Create a tab with text corresponding to the page title defined by the adapter. Also
86             // specify this Activity object, which implements the TabListener interface, as the
87             // callback (listener) for when this tab is selected.
88             actionBar.addTab(
89                     actionBar.newTab()
90                             .setText(mSectionsPagerAdapter.getPageTitle(i))
91                             .setTabListener(this));
92         }
93         // END_INCLUDE (add_tabs)
94     }
95 
96     /**
97      * Update {@link ViewPager} after a tab has been selected in the ActionBar.
98      *
99      * @param tab Tab that was selected.
100      * @param fragmentTransaction A {@link android.app.FragmentTransaction} for queuing fragment operations to
101      *                            execute once this method returns. This FragmentTransaction does
102      *                            not support being added to the back stack.
103      */
104     // BEGIN_INCLUDE (on_tab_selected)
105     @Override
onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)106     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
107         // When the given tab is selected, tell the ViewPager to switch to the corresponding page.
108         mViewPager.setCurrentItem(tab.getPosition());
109     }
110     // END_INCLUDE (on_tab_selected)
111 
112     /**
113      * Unused. Required for {@link android.app.ActionBar.TabListener}.
114      */
115     @Override
onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)116     public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
117     }
118 
119     /**
120      * Unused. Required for {@link android.app.ActionBar.TabListener}.
121      */
122     @Override
onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)123     public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
124     }
125 
126     // BEGIN_INCLUDE (fragment_pager_adapter)
127     /**
128      * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
129      * one of the sections/tabs/pages. This provides the data for the {@link ViewPager}.
130      */
131     public class SectionsPagerAdapter extends FragmentPagerAdapter {
132     // END_INCLUDE (fragment_pager_adapter)
133 
SectionsPagerAdapter(FragmentManager fm)134         public SectionsPagerAdapter(FragmentManager fm) {
135             super(fm);
136         }
137 
138         // BEGIN_INCLUDE (fragment_pager_adapter_getitem)
139         /**
140          * Get fragment corresponding to a specific position. This will be used to populate the
141          * contents of the {@link ViewPager}.
142          *
143          * @param position Position to fetch fragment for.
144          * @return Fragment for specified position.
145          */
146         @Override
getItem(int position)147         public Fragment getItem(int position) {
148             // getItem is called to instantiate the fragment for the given page.
149             // Return a DummySectionFragment (defined as a static inner class
150             // below) with the page number as its lone argument.
151             Fragment fragment = new DummySectionFragment();
152             Bundle args = new Bundle();
153             args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
154             fragment.setArguments(args);
155             return fragment;
156         }
157         // END_INCLUDE (fragment_pager_adapter_getitem)
158 
159         // BEGIN_INCLUDE (fragment_pager_adapter_getcount)
160         /**
161          * Get number of pages the {@link ViewPager} should render.
162          *
163          * @return Number of fragments to be rendered as pages.
164          */
165         @Override
getCount()166         public int getCount() {
167             // Show 3 total pages.
168             return 3;
169         }
170         // END_INCLUDE (fragment_pager_adapter_getcount)
171 
172         // BEGIN_INCLUDE (fragment_pager_adapter_getpagetitle)
173         /**
174          * Get title for each of the pages. This will be displayed on each of the tabs.
175          *
176          * @param position Page to fetch title for.
177          * @return Title for specified page.
178          */
179         @Override
getPageTitle(int position)180         public CharSequence getPageTitle(int position) {
181             Locale l = Locale.getDefault();
182             switch (position) {
183                 case 0:
184                     return getString(R.string.title_section1).toUpperCase(l);
185                 case 1:
186                     return getString(R.string.title_section2).toUpperCase(l);
187                 case 2:
188                     return getString(R.string.title_section3).toUpperCase(l);
189             }
190             return null;
191         }
192         // END_INCLUDE (fragment_pager_adapter_getpagetitle)
193     }
194 
195     /**
196      * A dummy fragment representing a section of the app, but that simply displays dummy text.
197      * This would be replaced with your application's content.
198      */
199     public static class DummySectionFragment extends Fragment {
200         /**
201          * The fragment argument representing the section number for this
202          * fragment.
203          */
204         public static final String ARG_SECTION_NUMBER = "section_number";
205 
DummySectionFragment()206         public DummySectionFragment() {
207         }
208 
209         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)210         public View onCreateView(LayoutInflater inflater, ViewGroup container,
211                 Bundle savedInstanceState) {
212             View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
213             TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
214             dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
215             return rootView;
216         }
217     }
218 
219 }
220