diff options
author | Roman Nurik <romannurik@google.com> | 2012-07-19 13:26:09 -0700 |
---|---|---|
committer | Roman Nurik <romannurik@google.com> | 2012-07-19 13:26:09 -0700 |
commit | be0d79958f7cc22317b18b8dad1d6cb7f3179b28 (patch) | |
tree | 6020b5b7fa03265dd179ac03228c2012ed0a5cdc /templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl | |
parent | 86e08822a1e435ce333dfbc5a5fea02aa7cee136 (diff) | |
download | sdk-be0d79958f7cc22317b18b8dad1d6cb7f3179b28.zip sdk-be0d79958f7cc22317b18b8dad1d6cb7f3179b28.tar.gz sdk-be0d79958f7cc22317b18b8dad1d6cb7f3179b28.tar.bz2 |
Activity templates cleanup.
This change cleans up the BlankActivity and MasterDetailFlow activity templates
in the following ways:
- Unused search icons are removed.
- Many comments are added for a better explanation of what's going on. See
http://code.google.com/p/android/issues/detail?id=34463
- Code and comments are in better alignment with Android code style and
import order (per http://source.android.com/source/code-style.html)
- Non-trivial BlankActivity templates now require API 11 instead of 14.
More work TBD here.
- Templates are more lint-friendly.
- android:parentActivityName is added when building against API 16.
Change-Id: Iba7b807c7567b161b0e50fb39306209172627c35
Diffstat (limited to 'templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl')
-rw-r--r-- | templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl | 106 |
1 files changed, 49 insertions, 57 deletions
diff --git a/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl index 58f6452..9e82449 100644 --- a/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl +++ b/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl @@ -1,8 +1,7 @@ package ${packageName}; -import android.app.ActionBar; -import android.app.FragmentTransaction; -import android.content.Context; +<#if navType?contains("tabs")>import android.app.ActionBar; +import android.app.FragmentTransaction;</#if> import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; @@ -21,10 +20,12 @@ import android.widget.TextView; public class ${activityClass} extends FragmentActivity<#if navType?contains("tabs")> implements ActionBar.TabListener</#if> { /** - * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the - * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will - * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best - * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. + * The {@link android.support.v4.view.PagerAdapter} that will provide + * fragments for each of the sections. We use a + * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which + * will keep every loaded fragment in memory. If this becomes too memory + * intensive, it may be best to switch to a + * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; @@ -37,27 +38,32 @@ public class ${activityClass} extends FragmentActivity<#if navType?contains("tab public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.${layoutName}); - <#if parentActivityClass != ""> - getActionBar().setDisplayHomeAsUpEnabled(true); - </#if> - // Create the adapter that will return a fragment for each of the three primary sections - // of the app. - mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); <#if navType?contains("tabs")> // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + <#if parentActivityClass != ""> + // Show the Up button in the action bar. + actionBar.setDisplayHomeAsUpEnabled(true); + </#if> + <#elseif parentActivityClass != ""> + // Show the Up button in the action bar. + getActionBar().setDisplayHomeAsUpEnabled(true); </#if> + // Create the adapter that will return a fragment for each of the three + // primary sections of the app. + mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); <#if navType?contains("tabs")> - // When swiping between different sections, select the corresponding tab. - // We can also use ActionBar.Tab#select() to do this if we have a reference to the - // Tab. + // When swiping between different sections, select the corresponding + // tab. We can also use ActionBar.Tab#select() to do this if we have + // a reference to the Tab. mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { @@ -67,9 +73,10 @@ public class ${activityClass} extends FragmentActivity<#if navType?contains("tab // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { - // Create a tab with text corresponding to the page title defined by the adapter. - // Also specify this Activity object, which implements the TabListener interface, as the - // listener for when this tab is selected. + // Create a tab with text corresponding to the page title defined by + // the adapter. Also specify this Activity object, which implements + // the TabListener interface, as the callback (listener) for when + // this tab is selected. actionBar.addTab( actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) @@ -78,32 +85,27 @@ public class ${activityClass} extends FragmentActivity<#if navType?contains("tab </#if> } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.${menuName}, menu); - return true; - } + <#include "include_onCreateOptionsMenu.java.ftl"> <#include "include_onOptionsItemSelected.java.ftl"> - <#if navType?contains("tabs")> - @Override - public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + <#if navType?contains("tabs")>@Override + public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + // When the given tab is selected, switch to the corresponding page in + // the ViewPager. + mViewPager.setCurrentItem(tab.getPosition()); } @Override - public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { - // When the given tab is selected, switch to the corresponding page in the ViewPager. - mViewPager.setCurrentItem(tab.getPosition()); + public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { - } - </#if> + }</#if> /** - * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary - * sections of the app. + * A {@link FragmentPagerAdapter} that returns a fragment corresponding to + * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { @@ -112,47 +114,37 @@ public class ${activityClass} extends FragmentActivity<#if navType?contains("tab } @Override - public Fragment getItem(int i) { + public Fragment getItem(int position) { + // getItem is called to instantiate the fragment for the given page. + // Return a DummySectionFragment (defined as a static inner class + // below) with the page number as its lone argument. Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); - args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); fragment.setArguments(args); return fragment; } @Override public int getCount() { + // Show 3 total pages. return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { - case 0: return getString(R.string.title_section1).toUpperCase(); - case 1: return getString(R.string.title_section2).toUpperCase(); - case 2: return getString(R.string.title_section3).toUpperCase(); + case 0: + return getString(R.string.title_section1).toUpperCase(); + case 1: + return getString(R.string.title_section2).toUpperCase(); + case 2: + return getString(R.string.title_section3).toUpperCase(); } return null; } } - /** - * A dummy fragment representing a section of the app, but that simply displays dummy text. - */ - public static class DummySectionFragment extends Fragment { - public DummySectionFragment() { - } - - public static final String ARG_SECTION_NUMBER = "section_number"; + <#include "include_DummySectionFragment.java.ftl"> - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - TextView textView = new TextView(getActivity()); - textView.setGravity(Gravity.CENTER); - Bundle args = getArguments(); - textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); - return textView; - } - } } |