1 package ${packageName}; 2 3 <#if navType?contains("tabs")>import android.app.ActionBar; 4 import android.app.FragmentTransaction;</#if> 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.app.NavUtils; 11 import android.support.v4.view.ViewPager; 12 import android.view.Gravity; 13 import android.view.LayoutInflater; 14 import android.view.Menu; 15 import android.view.MenuItem; 16 import android.view.View; 17 import android.view.ViewGroup; 18 import android.widget.TextView; 19 20 public class ${activityClass} extends FragmentActivity<#if navType?contains("tabs")> implements ActionBar.TabListener</#if> { 21 22 /** 23 * The {@link android.support.v4.view.PagerAdapter} that will provide 24 * fragments for each of the sections. We use a 25 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 26 * will keep every loaded fragment in memory. If this becomes too memory 27 * intensive, it may be best to switch to a 28 * {@link android.support.v4.app.FragmentStatePagerAdapter}. 29 */ 30 SectionsPagerAdapter mSectionsPagerAdapter; 31 32 /** 33 * The {@link ViewPager} that will host the section contents. 34 */ 35 ViewPager mViewPager; 36 37 @Override 38 public void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.${layoutName}); 41 42 <#if navType?contains("tabs")> 43 // Set up the action bar. 44 final ActionBar actionBar = getActionBar(); 45 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 46 <#if parentActivityClass != ""> 47 // Show the Up button in the action bar. 48 actionBar.setDisplayHomeAsUpEnabled(true); 49 </#if> 50 <#elseif parentActivityClass != ""> 51 // Show the Up button in the action bar. 52 getActionBar().setDisplayHomeAsUpEnabled(true); 53 </#if> 54 55 // Create the adapter that will return a fragment for each of the three 56 // primary sections of the app. 57 mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 58 59 // Set up the ViewPager with the sections adapter. 60 mViewPager = (ViewPager) findViewById(R.id.pager); 61 mViewPager.setAdapter(mSectionsPagerAdapter); 62 63 <#if navType?contains("tabs")> 64 // When swiping between different sections, select the corresponding 65 // tab. We can also use ActionBar.Tab#select() to do this if we have 66 // a reference to the Tab. 67 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 68 @Override 69 public void onPageSelected(int position) { 70 actionBar.setSelectedNavigationItem(position); 71 } 72 }); 73 74 // For each of the sections in the app, add a tab to the action bar. 75 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 76 // Create a tab with text corresponding to the page title defined by 77 // the adapter. Also specify this Activity object, which implements 78 // the TabListener interface, as the callback (listener) for when 79 // this tab is selected. 80 actionBar.addTab( 81 actionBar.newTab() 82 .setText(mSectionsPagerAdapter.getPageTitle(i)) 83 .setTabListener(this)); 84 } 85 </#if> 86 } 87 88 <#include "include_onCreateOptionsMenu.java.ftl"> 89 <#include "include_onOptionsItemSelected.java.ftl"> 90 91 <#if navType?contains("tabs")>@Override 92 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 93 // When the given tab is selected, switch to the corresponding page in 94 // the ViewPager. 95 mViewPager.setCurrentItem(tab.getPosition()); 96 } 97 98 @Override 99 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 100 } 101 102 @Override 103 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 104 }</#if> 105 106 /** 107 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 108 * one of the sections/tabs/pages. 109 */ 110 public class SectionsPagerAdapter extends FragmentPagerAdapter { 111 112 public SectionsPagerAdapter(FragmentManager fm) { 113 super(fm); 114 } 115 116 @Override 117 public Fragment getItem(int position) { 118 // getItem is called to instantiate the fragment for the given page. 119 // Return a DummySectionFragment (defined as a static inner class 120 // below) with the page number as its lone argument. 121 Fragment fragment = new DummySectionFragment(); 122 Bundle args = new Bundle(); 123 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 124 fragment.setArguments(args); 125 return fragment; 126 } 127 128 @Override 129 public int getCount() { 130 // Show 3 total pages. 131 return 3; 132 } 133 134 @Override 135 public CharSequence getPageTitle(int position) { 136 switch (position) { 137 case 0: 138 return getString(R.string.title_section1).toUpperCase(); 139 case 1: 140 return getString(R.string.title_section2).toUpperCase(); 141 case 2: 142 return getString(R.string.title_section3).toUpperCase(); 143 } 144 return null; 145 } 146 } 147 148 <#include "include_DummySectionFragment.java.ftl"> 149 150 } 151