Debian Codegraph @

templates/eclipse/activities/TabbedActivity/root/src/app_package/TabsAndPagerActivity.java.ftl

raw
1 package ${packageName};
2
3 import java.util.Locale;
4
5 import <#if appCompat>android.support.v7.app.ActionBarActivity<#else>android.app.Activity</#if>;
6 import android.<#if appCompat>support.v7.</#if>app.ActionBar;
7 import android.<#if appCompat>support.v4.</#if>app.Fragment;
8 import android.<#if appCompat>support.v4.</#if>app.FragmentManager;
9 import android.<#if appCompat>support.v4.</#if>app.FragmentTransaction;
10 import android.support.${(appCompat)?string('v4','v13')}.app.FragmentPagerAdapter;
11 import android.os.Bundle;
12 import android.support.v4.view.ViewPager;
13 import android.view.Gravity;
14 import android.view.LayoutInflater;
15 import android.view.Menu;
16 import android.view.MenuItem;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.TextView;
20 <#if applicationPackage??>import ${applicationPackage}.R;</#if>
21
22 public class ${activityClass} extends ${(appCompat)?string('ActionBar','')}Activity<#if features == '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 FragmentPagerAdapter} derivative, which will keep every
28 * loaded fragment in memory. If this becomes too memory intensive, it
29 * may be best to switch to a
30 * {@link android.support.${(appCompat)?string('v4','v13')}.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 features == 'tabs'>
45 // Set up the action bar.
46 final ActionBar actionBar = get${Support}ActionBar();
47 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);</#if>
48
49 // Create the adapter that will return a fragment for each of the three
50 // primary sections of the activity.
51 mSectionsPagerAdapter = new SectionsPagerAdapter(get${Support}FragmentManager());
52
53 // Set up the ViewPager with the sections adapter.
54 mViewPager = (ViewPager) findViewById(R.id.pager);
55 mViewPager.setAdapter(mSectionsPagerAdapter);
56
57 <#if features == 'tabs'>
58 // When swiping between different sections, select the corresponding
59 // tab. We can also use ActionBar.Tab#select() to do this if we have
60 // a reference to the Tab.
61 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
62 @Override
63 public void onPageSelected(int position) {
64 actionBar.setSelectedNavigationItem(position);
65 }
66 });
67
68 // For each of the sections in the app, add a tab to the action bar.
69 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
70 // Create a tab with text corresponding to the page title defined by
71 // the adapter. Also specify this Activity object, which implements
72 // the TabListener interface, as the callback (listener) for when
73 // this tab is selected.
74 actionBar.addTab(
75 actionBar.newTab()
76 .setText(mSectionsPagerAdapter.getPageTitle(i))
77 .setTabListener(this));
78 }
79 </#if>
80 }
81
82 <#include "include_options_menu.java.ftl">
83
84 <#if features == 'tabs'>@Override
85 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
86 // When the given tab is selected, switch to the corresponding page in
87 // the ViewPager.
88 mViewPager.setCurrentItem(tab.getPosition());
89 }
90
91 @Override
92 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
93 }
94
95 @Override
96 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
97 }</#if>
98
99 /**
100 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
101 * one of the sections/tabs/pages.
102 */
103 public class SectionsPagerAdapter extends FragmentPagerAdapter {
104
105 public SectionsPagerAdapter(FragmentManager fm) {
106 super(fm);
107 }
108
109 @Override
110 public Fragment getItem(int position) {
111 // getItem is called to instantiate the fragment for the given page.
112 // Return a PlaceholderFragment (defined as a static inner class below).
113 return PlaceholderFragment.newInstance(position + 1);
114 }
115
116 @Override
117 public int getCount() {
118 // Show 3 total pages.
119 return 3;
120 }
121
122 @Override
123 public CharSequence getPageTitle(int position) {
124 Locale l = Locale.getDefault();
125 switch (position) {
126 case 0:
127 return getString(R.string.title_section1).toUpperCase(l);
128 case 1:
129 return getString(R.string.title_section2).toUpperCase(l);
130 case 2:
131 return getString(R.string.title_section3).toUpperCase(l);
132 }
133 return null;
134 }
135 }
136
137 <#include "include_fragment.java.ftl">
138
139 }