Home | History | Annotate | Download | only in effectivenavigation
      1 /*
      2  * Copyright 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.effectivenavigation;
     18 
     19 import android.app.ActionBar;
     20 import android.app.FragmentTransaction;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.support.v4.app.Fragment;
     24 import android.support.v4.app.FragmentActivity;
     25 import android.support.v4.app.FragmentManager;
     26 import android.support.v4.app.FragmentPagerAdapter;
     27 import android.support.v4.view.ViewPager;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.TextView;
     32 
     33 public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
     34 
     35     /**
     36      * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     37      * three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter}
     38      * derivative, which will keep every loaded fragment in memory. If this becomes too memory
     39      * intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     40      */
     41     AppSectionsPagerAdapter mAppSectionsPagerAdapter;
     42 
     43     /**
     44      * The {@link ViewPager} that will display the three primary sections of the app, one at a
     45      * time.
     46      */
     47     ViewPager mViewPager;
     48 
     49     public void onCreate(Bundle savedInstanceState) {
     50         super.onCreate(savedInstanceState);
     51         setContentView(R.layout.activity_main);
     52 
     53         // Create the adapter that will return a fragment for each of the three primary sections
     54         // of the app.
     55         mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
     56 
     57         // Set up the action bar.
     58         final ActionBar actionBar = getActionBar();
     59 
     60         // Specify that the Home/Up button should not be enabled, since there is no hierarchical
     61         // parent.
     62         actionBar.setHomeButtonEnabled(false);
     63 
     64         // Specify that we will be displaying tabs in the action bar.
     65         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     66 
     67         // Set up the ViewPager, attaching the adapter and setting up a listener for when the
     68         // user swipes between sections.
     69         mViewPager = (ViewPager) findViewById(R.id.pager);
     70         mViewPager.setAdapter(mAppSectionsPagerAdapter);
     71         mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
     72             @Override
     73             public void onPageSelected(int position) {
     74                 // When swiping between different app sections, select the corresponding tab.
     75                 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
     76                 // Tab.
     77                 actionBar.setSelectedNavigationItem(position);
     78             }
     79         });
     80 
     81         // For each of the sections in the app, add a tab to the action bar.
     82         for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
     83             // Create a tab with text corresponding to the page title defined by the adapter.
     84             // Also specify this Activity object, which implements the TabListener interface, as the
     85             // listener for when this tab is selected.
     86             actionBar.addTab(
     87                     actionBar.newTab()
     88                             .setText(mAppSectionsPagerAdapter.getPageTitle(i))
     89                             .setTabListener(this));
     90         }
     91     }
     92 
     93     @Override
     94     public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
     95     }
     96 
     97     @Override
     98     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
     99         // When the given tab is selected, switch to the corresponding page in the ViewPager.
    100         mViewPager.setCurrentItem(tab.getPosition());
    101     }
    102 
    103     @Override
    104     public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    105     }
    106 
    107     /**
    108      * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
    109      * sections of the app.
    110      */
    111     public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
    112 
    113         public AppSectionsPagerAdapter(FragmentManager fm) {
    114             super(fm);
    115         }
    116 
    117         @Override
    118         public Fragment getItem(int i) {
    119             switch (i) {
    120                 case 0:
    121                     // The first section of the app is the most interesting -- it offers
    122                     // a launchpad into the other demonstrations in this example application.
    123                     return new LaunchpadSectionFragment();
    124 
    125                 default:
    126                     // The other sections of the app are dummy placeholders.
    127                     Fragment fragment = new DummySectionFragment();
    128                     Bundle args = new Bundle();
    129                     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
    130                     fragment.setArguments(args);
    131                     return fragment;
    132             }
    133         }
    134 
    135         @Override
    136         public int getCount() {
    137             return 3;
    138         }
    139 
    140         @Override
    141         public CharSequence getPageTitle(int position) {
    142             return "Section " + (position + 1);
    143         }
    144     }
    145 
    146     /**
    147      * A fragment that launches other parts of the demo application.
    148      */
    149     public static class LaunchpadSectionFragment extends Fragment {
    150 
    151         @Override
    152         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    153                 Bundle savedInstanceState) {
    154             View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
    155 
    156             // Demonstration of a collection-browsing activity.
    157             rootView.findViewById(R.id.demo_collection_button)
    158                     .setOnClickListener(new View.OnClickListener() {
    159                         @Override
    160                         public void onClick(View view) {
    161                             Intent intent = new Intent(getActivity(), CollectionDemoActivity.class);
    162                             startActivity(intent);
    163                         }
    164                     });
    165 
    166             // Demonstration of navigating to external activities.
    167             rootView.findViewById(R.id.demo_external_activity)
    168                     .setOnClickListener(new View.OnClickListener() {
    169                         @Override
    170                         public void onClick(View view) {
    171                             // Create an intent that asks the user to pick a photo, but using
    172                             // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching
    173                             // the application from the device home screen does not return
    174                             // to the external activity.
    175                             Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
    176                             externalActivityIntent.setType("image/*");
    177                             externalActivityIntent.addFlags(
    178                                     Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    179                             startActivity(externalActivityIntent);
    180                         }
    181                     });
    182 
    183             return rootView;
    184         }
    185     }
    186 
    187     /**
    188      * A dummy fragment representing a section of the app, but that simply displays dummy text.
    189      */
    190     public static class DummySectionFragment extends Fragment {
    191 
    192         public static final String ARG_SECTION_NUMBER = "section_number";
    193 
    194         @Override
    195         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    196                 Bundle savedInstanceState) {
    197             View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
    198             Bundle args = getArguments();
    199             ((TextView) rootView.findViewById(android.R.id.text1)).setText(
    200                     getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
    201             return rootView;
    202         }
    203     }
    204 }
    205