| 1 |
package ${packageName}; |
| 2 |
|
| 3 |
<#if hasAppBar> |
| 4 |
<#if features == 'tabs'> |
| 5 |
import android.support.design.widget.TabLayout; |
| 6 |
</#if> |
| 7 |
import android.support.design.widget.FloatingActionButton; |
| 8 |
import android.support.design.widget.Snackbar; |
| 9 |
import android.support.v7.app.AppCompatActivity; |
| 10 |
import android.support.v7.widget.Toolbar; |
| 11 |
<#else> <#-- hasAppBar --> |
| 12 |
import ${superClassFqcn}; |
| 13 |
import android.<#if appCompat>support.v7.</#if>app.ActionBar; |
| 14 |
import android.<#if appCompat>support.v4.</#if>app.FragmentTransaction; |
| 15 |
</#if> <#-- hasAppBar --> |
| 16 |
import android.<#if appCompat>support.v4.</#if>app.Fragment; |
| 17 |
<#if hasViewPager> |
| 18 |
import android.<#if appCompat>support.v4.</#if>app.FragmentManager; |
| 19 |
import android.support.${(appCompat)?string('v4','v13')}.app.FragmentPagerAdapter; |
| 20 |
import android.support.v4.view.ViewPager; |
| 21 |
</#if> |
| 22 |
import android.os.Bundle; |
| 23 |
import android.view.LayoutInflater; |
| 24 |
import android.view.Menu; |
| 25 |
import android.view.MenuItem; |
| 26 |
import android.view.View; |
| 27 |
import android.view.ViewGroup; |
| 28 |
<#if features == 'spinner'> |
| 29 |
import android.widget.AdapterView; |
| 30 |
import android.widget.AdapterView.OnItemSelectedListener; |
| 31 |
import android.widget.ArrayAdapter; |
| 32 |
import android.widget.Spinner; |
| 33 |
import android.content.Context; |
| 34 |
<#if buildApi gte 23> |
| 35 |
import android.support.v7.widget.ThemedSpinnerAdapter; |
| 36 |
import android.content.res.Resources.Theme; |
| 37 |
<#else> |
| 38 |
import android.graphics.Color; |
| 39 |
</#if> |
| 40 |
</#if> <#-- features == 'spinner' --> |
| 41 |
import android.widget.TextView; |
| 42 |
<#if applicationPackage??> |
| 43 |
import ${applicationPackage}.R; |
| 44 |
</#if> |
| 45 |
|
| 46 |
public class ${activityClass} extends ${superClass}<#if !hasAppBar && features == 'tabs'> implements ActionBar.TabListener</#if> { |
| 47 |
|
| 48 |
<#if hasViewPager> |
| 49 |
/** |
| 50 |
* The {@link android.support.v4.view.PagerAdapter} that will provide |
| 51 |
* fragments for each of the sections. We use a |
| 52 |
* {@link FragmentPagerAdapter} derivative, which will keep every |
| 53 |
* loaded fragment in memory. If this becomes too memory intensive, it |
| 54 |
* may be best to switch to a |
| 55 |
* {@link android.support.${(appCompat)?string('v4','v13')}.app.FragmentStatePagerAdapter}. |
| 56 |
*/ |
| 57 |
private SectionsPagerAdapter mSectionsPagerAdapter; |
| 58 |
|
| 59 |
/** |
| 60 |
* The {@link ViewPager} that will host the section contents. |
| 61 |
*/ |
| 62 |
private ViewPager mViewPager; |
| 63 |
|
| 64 |
</#if> |
| 65 |
@Override |
| 66 |
protected void onCreate(Bundle savedInstanceState) { |
| 67 |
super.onCreate(savedInstanceState); |
| 68 |
setContentView(R.layout.${layoutName}); |
| 69 |
<#if hasAppBar> |
| 70 |
|
| 71 |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 72 |
setSupportActionBar(toolbar); |
| 73 |
</#if> |
| 74 |
<#if parentActivityClass != ""> |
| 75 |
get${Support}ActionBar().setDisplayHomeAsUpEnabled(true); |
| 76 |
</#if> |
| 77 |
<#if hasViewPager> |
| 78 |
// Create the adapter that will return a fragment for each of the three |
| 79 |
// primary sections of the activity. |
| 80 |
mSectionsPagerAdapter = new SectionsPagerAdapter(get${Support}FragmentManager()); |
| 81 |
|
| 82 |
// Set up the ViewPager with the sections adapter. |
| 83 |
mViewPager = (ViewPager) findViewById(R.id.container); |
| 84 |
mViewPager.setAdapter(mSectionsPagerAdapter); |
| 85 |
|
| 86 |
</#if> |
| 87 |
<#if hasAppBar> |
| 88 |
<#if features == 'tabs'> |
| 89 |
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); |
| 90 |
tabLayout.setupWithViewPager(mViewPager); |
| 91 |
<#elseif features == 'spinner'> |
| 92 |
getSupportActionBar().setDisplayShowTitleEnabled(false); |
| 93 |
|
| 94 |
// Setup spinner |
| 95 |
Spinner spinner = (Spinner) findViewById(R.id.spinner); |
| 96 |
spinner.setAdapter(new MyAdapter( |
| 97 |
toolbar.getContext(), |
| 98 |
new String[]{ |
| 99 |
"Section 1", |
| 100 |
"Section 2", |
| 101 |
"Section 3", |
| 102 |
})); |
| 103 |
|
| 104 |
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { |
| 105 |
@Override |
| 106 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 107 |
// When the given dropdown item is selected, show its contents in the |
| 108 |
// container view. |
| 109 |
get${Support}FragmentManager().beginTransaction() |
| 110 |
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) |
| 111 |
.commit(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void onNothingSelected(AdapterView<?> parent) { |
| 116 |
} |
| 117 |
}); |
| 118 |
</#if> |
| 119 |
|
| 120 |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
| 121 |
fab.setOnClickListener(new View.OnClickListener() { |
| 122 |
@Override |
| 123 |
public void onClick(View view) { |
| 124 |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) |
| 125 |
.setAction("Action", null).show(); |
| 126 |
} |
| 127 |
}); |
| 128 |
<#else> <#-- hasAppBar --> |
| 129 |
<#if features == 'tabs'> |
| 130 |
// Set up the action bar. |
| 131 |
final ActionBar actionBar = get${Support}ActionBar(); |
| 132 |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 133 |
|
| 134 |
// When swiping between different sections, select the corresponding |
| 135 |
// tab. We can also use ActionBar.Tab#select() to do this if we have |
| 136 |
// a reference to the Tab. |
| 137 |
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { |
| 138 |
@Override |
| 139 |
public void onPageSelected(int position) { |
| 140 |
actionBar.setSelectedNavigationItem(position); |
| 141 |
} |
| 142 |
}); |
| 143 |
|
| 144 |
// For each of the sections in the app, add a tab to the action bar. |
| 145 |
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { |
| 146 |
// Create a tab with text corresponding to the page title defined by |
| 147 |
// the adapter. Also specify this Activity object, which implements |
| 148 |
// the TabListener interface, as the callback (listener) for when |
| 149 |
// this tab is selected. |
| 150 |
actionBar.addTab( |
| 151 |
actionBar.newTab() |
| 152 |
.setText(mSectionsPagerAdapter.getPageTitle(i)) |
| 153 |
.setTabListener(this)); |
| 154 |
} |
| 155 |
</#if> |
| 156 |
</#if> <#-- hasAppBar --> |
| 157 |
} |
| 158 |
|
| 159 |
<#include "include_options_menu.java.ftl"> |
| 160 |
|
| 161 |
<#if !hasAppBar && features == 'tabs'>@Override |
| 162 |
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { |
| 163 |
// When the given tab is selected, switch to the corresponding page in |
| 164 |
// the ViewPager. |
| 165 |
mViewPager.setCurrentItem(tab.getPosition()); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { |
| 174 |
} |
| 175 |
</#if> |
| 176 |
|
| 177 |
<#if features == 'spinner'> |
| 178 |
<#if buildApi gte 23> |
| 179 |
private static class MyAdapter extends ArrayAdapter<String> implements ThemedSpinnerAdapter { |
| 180 |
private final ThemedSpinnerAdapter.Helper mDropDownHelper; |
| 181 |
|
| 182 |
public MyAdapter(Context context, String[] objects) { |
| 183 |
super(context, android.R.layout.simple_list_item_1, objects); |
| 184 |
mDropDownHelper = new ThemedSpinnerAdapter.Helper(context); |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 189 |
View view; |
| 190 |
|
| 191 |
if (convertView == null) { |
| 192 |
// Inflate the drop down using the helper's LayoutInflater |
| 193 |
LayoutInflater inflater = mDropDownHelper.getDropDownViewInflater(); |
| 194 |
view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); |
| 195 |
} else { |
| 196 |
view = convertView; |
| 197 |
} |
| 198 |
|
| 199 |
TextView textView = (TextView) view.findViewById(android.R.id.text1); |
| 200 |
textView.setText(getItem(position)); |
| 201 |
|
| 202 |
return view; |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void setDropDownViewTheme(Theme theme) { |
| 207 |
mDropDownHelper.setDropDownViewTheme(theme); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public Theme getDropDownViewTheme() { |
| 212 |
return mDropDownHelper.getDropDownViewTheme(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
<#else> <#-- buildApi gte 23 --> |
| 217 |
private static class MyAdapter extends ArrayAdapter<String> { |
| 218 |
|
| 219 |
public MyAdapter(Context context, String[] objects) { |
| 220 |
super(context, android.R.layout.simple_list_item_1, android.R.id.text1, objects); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 225 |
View view = super.getView(position, convertView, parent); |
| 226 |
TextView text = (TextView) view.findViewById(android.R.id.text1); |
| 227 |
// Hack. Use BuildVersion 23 for a better approach. |
| 228 |
text.setTextColor(Color.BLACK); |
| 229 |
text.setBackgroundColor(Color.WHITE); |
| 230 |
return view; |
| 231 |
} |
| 232 |
} |
| 233 |
</#if> |
| 234 |
</#if> <#-- features == 'spinner' --> |
| 235 |
|
| 236 |
<#if hasViewPager> |
| 237 |
/** |
| 238 |
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to |
| 239 |
* one of the sections/tabs/pages. |
| 240 |
*/ |
| 241 |
public class SectionsPagerAdapter extends FragmentPagerAdapter { |
| 242 |
|
| 243 |
public SectionsPagerAdapter(FragmentManager fm) { |
| 244 |
super(fm); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public Fragment getItem(int position) { |
| 249 |
// getItem is called to instantiate the fragment for the given page. |
| 250 |
// Return a PlaceholderFragment (defined as a static inner class below). |
| 251 |
return PlaceholderFragment.newInstance(position + 1); |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public int getCount() { |
| 256 |
// Show 3 total pages. |
| 257 |
return 3; |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public CharSequence getPageTitle(int position) { |
| 262 |
switch (position) { |
| 263 |
case 0: |
| 264 |
return "SECTION 1"; |
| 265 |
case 1: |
| 266 |
return "SECTION 2"; |
| 267 |
case 2: |
| 268 |
return "SECTION 3"; |
| 269 |
} |
| 270 |
return null; |
| 271 |
} |
| 272 |
} |
| 273 |
</#if> |
| 274 |
|
| 275 |
<#include "include_fragment.java.ftl"> |
| 276 |
} |