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