summaryrefslogtreecommitdiffstats
path: root/samples/Support13Demos/src
diff options
context:
space:
mode:
Diffstat (limited to 'samples/Support13Demos/src')
-rw-r--r--samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java3
-rw-r--r--samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java159
-rw-r--r--samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java159
-rw-r--r--samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java187
4 files changed, 506 insertions, 2 deletions
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java b/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java
index 8672ed22a..d52955b21 100644
--- a/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java
@@ -58,10 +58,9 @@ public class CountingFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.hello_world, container, false);
+ View v = inflater.inflate(R.layout.counting, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
- tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java
new file mode 100644
index 000000000..0a88a0507
--- /dev/null
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.supportv13.app;
+
+import java.util.ArrayList;
+
+import com.example.android.supportv13.R;
+
+import android.app.ActionBar;
+import android.app.ActionBar.Tab;
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v13.app.FragmentPagerAdapter;
+import android.support.v4.view.ViewPager;
+
+//BEGIN_INCLUDE(complete)
+public class FragmentNestingPagerSupport extends Activity {
+ ViewPager mViewPager;
+ TabsAdapter mTabsAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mViewPager = new ViewPager(this);
+ mViewPager.setId(R.id.pager);
+ setContentView(mViewPager);
+
+ final ActionBar bar = getActionBar();
+ bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+ bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
+
+ mTabsAdapter = new TabsAdapter(this, mViewPager);
+ mTabsAdapter.addTab(bar.newTab().setText("Simple"),
+ CountingFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("List"),
+ FragmentPagerSupport.ArrayListFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Cursor"),
+ CursorFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Tabs"),
+ FragmentTabsFragment.class, null);
+
+ if (savedInstanceState != null) {
+ bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
+ }
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
+ }
+
+ /**
+ * This is a helper class that implements the management of tabs and all
+ * details of connecting a ViewPager with associated TabHost. It relies on a
+ * trick. Normally a tab host has a simple API for supplying a View or
+ * Intent that each tab will show. This is not sufficient for switching
+ * between pages. So instead we make the content part of the tab host
+ * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
+ * view to show as the tab content. It listens to changes in tabs, and takes
+ * care of switch to the correct paged in the ViewPager whenever the selected
+ * tab changes.
+ */
+ public static class TabsAdapter extends FragmentPagerAdapter
+ implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
+ private final Context mContext;
+ private final ActionBar mActionBar;
+ private final ViewPager mViewPager;
+ private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
+
+ static final class TabInfo {
+ private final Class<?> clss;
+ private final Bundle args;
+
+ TabInfo(Class<?> _class, Bundle _args) {
+ clss = _class;
+ args = _args;
+ }
+ }
+
+ public TabsAdapter(Activity activity, ViewPager pager) {
+ super(activity.getFragmentManager());
+ mContext = activity;
+ mActionBar = activity.getActionBar();
+ mViewPager = pager;
+ mViewPager.setAdapter(this);
+ mViewPager.setOnPageChangeListener(this);
+ }
+
+ public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
+ TabInfo info = new TabInfo(clss, args);
+ tab.setTag(info);
+ tab.setTabListener(this);
+ mTabs.add(info);
+ mActionBar.addTab(tab);
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getCount() {
+ return mTabs.size();
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ TabInfo info = mTabs.get(position);
+ return Fragment.instantiate(mContext, info.clss.getName(), info.args);
+ }
+
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ mActionBar.setSelectedNavigationItem(position);
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+ }
+
+ @Override
+ public void onTabSelected(Tab tab, FragmentTransaction ft) {
+ Object tag = tab.getTag();
+ for (int i=0; i<mTabs.size(); i++) {
+ if (mTabs.get(i) == tag) {
+ mViewPager.setCurrentItem(i);
+ }
+ }
+ }
+
+ @Override
+ public void onTabUnselected(Tab tab, FragmentTransaction ft) {
+ }
+
+ @Override
+ public void onTabReselected(Tab tab, FragmentTransaction ft) {
+ }
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java
new file mode 100644
index 000000000..58638528b
--- /dev/null
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.supportv13.app;
+
+import java.util.ArrayList;
+
+import com.example.android.supportv13.R;
+
+import android.app.ActionBar;
+import android.app.ActionBar.Tab;
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v13.app.FragmentStatePagerAdapter;
+import android.support.v4.view.ViewPager;
+
+//BEGIN_INCLUDE(complete)
+public class FragmentNestingStatePagerSupport extends Activity {
+ ViewPager mViewPager;
+ TabsAdapter mTabsAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mViewPager = new ViewPager(this);
+ mViewPager.setId(R.id.pager);
+ setContentView(mViewPager);
+
+ final ActionBar bar = getActionBar();
+ bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+ bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
+
+ mTabsAdapter = new TabsAdapter(this, mViewPager);
+ mTabsAdapter.addTab(bar.newTab().setText("Simple"),
+ CountingFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("List"),
+ FragmentPagerSupport.ArrayListFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Cursor"),
+ CursorFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Tabs"),
+ FragmentTabsFragment.class, null);
+
+ if (savedInstanceState != null) {
+ bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
+ }
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
+ }
+
+ /**
+ * This is a helper class that implements the management of tabs and all
+ * details of connecting a ViewPager with associated TabHost. It relies on a
+ * trick. Normally a tab host has a simple API for supplying a View or
+ * Intent that each tab will show. This is not sufficient for switching
+ * between pages. So instead we make the content part of the tab host
+ * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
+ * view to show as the tab content. It listens to changes in tabs, and takes
+ * care of switch to the correct paged in the ViewPager whenever the selected
+ * tab changes.
+ */
+ public static class TabsAdapter extends FragmentStatePagerAdapter
+ implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
+ private final Context mContext;
+ private final ActionBar mActionBar;
+ private final ViewPager mViewPager;
+ private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
+
+ static final class TabInfo {
+ private final Class<?> clss;
+ private final Bundle args;
+
+ TabInfo(Class<?> _class, Bundle _args) {
+ clss = _class;
+ args = _args;
+ }
+ }
+
+ public TabsAdapter(Activity activity, ViewPager pager) {
+ super(activity.getFragmentManager());
+ mContext = activity;
+ mActionBar = activity.getActionBar();
+ mViewPager = pager;
+ mViewPager.setAdapter(this);
+ mViewPager.setOnPageChangeListener(this);
+ }
+
+ public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
+ TabInfo info = new TabInfo(clss, args);
+ tab.setTag(info);
+ tab.setTabListener(this);
+ mTabs.add(info);
+ mActionBar.addTab(tab);
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getCount() {
+ return mTabs.size();
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ TabInfo info = mTabs.get(position);
+ return Fragment.instantiate(mContext, info.clss.getName(), info.args);
+ }
+
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ mActionBar.setSelectedNavigationItem(position);
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+ }
+
+ @Override
+ public void onTabSelected(Tab tab, FragmentTransaction ft) {
+ Object tag = tab.getTag();
+ for (int i=0; i<mTabs.size(); i++) {
+ if (mTabs.get(i) == tag) {
+ mViewPager.setCurrentItem(i);
+ }
+ }
+ }
+
+ @Override
+ public void onTabUnselected(Tab tab, FragmentTransaction ft) {
+ }
+
+ @Override
+ public void onTabReselected(Tab tab, FragmentTransaction ft) {
+ }
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java
new file mode 100644
index 000000000..fbc464dda
--- /dev/null
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.supportv13.app;
+
+import java.util.HashMap;
+
+import com.example.android.supportv13.R;
+
+import android.app.Fragment;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.content.Context;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TabHost;
+
+public class FragmentTabsFragment extends Fragment {
+ TabHost mTabHost;
+ TabManager mTabManager;
+ String mCurrentTabTag;
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_tabs_fragment, container, false);
+ mTabHost = (TabHost)v.findViewById(android.R.id.tabhost);
+ mTabHost.setup();
+
+ mTabManager = new TabManager(getActivity(), getChildFragmentManager(),
+ mTabHost, R.id.realtabcontent);
+
+ mTabManager.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
+ CountingFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("array").setIndicator("Array"),
+ FragmentPagerSupport.ArrayListFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("cursor").setIndicator("Cursor"),
+ CursorFragment.class, null);
+
+ return v;
+ }
+
+ @Override
+ public void onViewStateRestored(Bundle savedInstanceState) {
+ super.onViewStateRestored(savedInstanceState);
+ if (savedInstanceState != null) {
+ mCurrentTabTag = savedInstanceState.getString("tab");
+ }
+ mTabHost.setCurrentTabByTag(mCurrentTabTag);
+ }
+
+ @Override
+ public void onDestroyView() {
+ super.onDestroyView();
+ // Need to remember the selected tab so that we can restore it if
+ // we later re-create the views.
+ mCurrentTabTag = mTabHost.getCurrentTabTag();
+ mTabHost = null;
+ mTabManager = null;
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putString("tab", mTabHost != null
+ ? mTabHost.getCurrentTabTag() : mCurrentTabTag);
+ }
+
+ /**
+ * This is a helper class that implements a generic mechanism for
+ * associating fragments with the tabs in a tab host. It relies on a
+ * trick. Normally a tab host has a simple API for supplying a View or
+ * Intent that each tab will show. This is not sufficient for switching
+ * between fragments. So instead we make the content part of the tab host
+ * 0dp high (it is not shown) and the TabManager supplies its own dummy
+ * view to show as the tab content. It listens to changes in tabs, and takes
+ * care of switch to the correct fragment shown in a separate content area
+ * whenever the selected tab changes.
+ */
+ public static class TabManager implements TabHost.OnTabChangeListener {
+ private final Context mContext;
+ private final FragmentManager mManager;
+ private final TabHost mTabHost;
+ private final int mContainerId;
+ private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
+ TabInfo mLastTab;
+
+ static final class TabInfo {
+ private final String tag;
+ private final Class<?> clss;
+ private final Bundle args;
+ private Fragment fragment;
+
+ TabInfo(String _tag, Class<?> _class, Bundle _args) {
+ tag = _tag;
+ clss = _class;
+ args = _args;
+ }
+ }
+
+ static class DummyTabFactory implements TabHost.TabContentFactory {
+ private final Context mContext;
+
+ public DummyTabFactory(Context context) {
+ mContext = context;
+ }
+
+ @Override
+ public View createTabContent(String tag) {
+ View v = new View(mContext);
+ v.setMinimumWidth(0);
+ v.setMinimumHeight(0);
+ return v;
+ }
+ }
+
+ public TabManager(Context context, FragmentManager manager, TabHost tabHost,
+ int containerId) {
+ mContext = context;
+ mManager = manager;
+ mTabHost = tabHost;
+ mContainerId = containerId;
+ mTabHost.setOnTabChangedListener(this);
+ }
+
+ public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
+ tabSpec.setContent(new DummyTabFactory(mContext));
+ String tag = tabSpec.getTag();
+
+ TabInfo info = new TabInfo(tag, clss, args);
+
+ // Check to see if we already have a fragment for this tab, probably
+ // from a previously saved state. If so, deactivate it, because our
+ // initial state is that a tab isn't shown.
+ info.fragment = mManager.findFragmentByTag(tag);
+ if (info.fragment != null && !info.fragment.isDetached()) {
+ FragmentTransaction ft = mManager.beginTransaction();
+ ft.detach(info.fragment);
+ ft.commit();
+ }
+
+ mTabs.put(tag, info);
+ mTabHost.addTab(tabSpec);
+ }
+
+ @Override
+ public void onTabChanged(String tabId) {
+ TabInfo newTab = mTabs.get(tabId);
+ if (mLastTab != newTab) {
+ FragmentTransaction ft = mManager.beginTransaction();
+ if (mLastTab != null) {
+ if (mLastTab.fragment != null) {
+ ft.detach(mLastTab.fragment);
+ }
+ }
+ if (newTab != null) {
+ if (newTab.fragment == null) {
+ newTab.fragment = Fragment.instantiate(mContext,
+ newTab.clss.getName(), newTab.args);
+ ft.add(mContainerId, newTab.fragment, newTab.tag);
+ } else {
+ ft.attach(newTab.fragment);
+ }
+ }
+
+ mLastTab = newTab;
+ ft.commit();
+ mManager.executePendingTransactions();
+ }
+ }
+ }
+}
+//END_INCLUDE(complete)