1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.example.android.supportv13.app;
17
18import java.util.ArrayList;
19
20import com.example.android.supportv13.R;
21
22import android.app.ActionBar;
23import android.app.ActionBar.Tab;
24import android.app.Activity;
25import android.app.Fragment;
26import android.app.FragmentTransaction;
27import android.content.Context;
28import android.os.Bundle;
29import android.support.v13.app.FragmentPagerAdapter;
30import android.support.v4.view.ViewPager;
31
32//BEGIN_INCLUDE(complete)
33public class FragmentNestingPagerSupport extends Activity {
34    ViewPager mViewPager;
35    TabsAdapter mTabsAdapter;
36
37    @Override
38    protected void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40
41        mViewPager = new ViewPager(this);
42        mViewPager.setId(R.id.pager);
43        setContentView(mViewPager);
44
45        final ActionBar bar = getActionBar();
46        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
47        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
48
49        mTabsAdapter = new TabsAdapter(this, mViewPager);
50        mTabsAdapter.addTab(bar.newTab().setText("Simple"),
51                CountingFragment.class, null);
52        mTabsAdapter.addTab(bar.newTab().setText("List"),
53                FragmentPagerSupport.ArrayListFragment.class, null);
54        mTabsAdapter.addTab(bar.newTab().setText("Cursor"),
55                CursorFragment.class, null);
56        mTabsAdapter.addTab(bar.newTab().setText("Tabs"),
57                FragmentTabsFragment.class, null);
58
59        if (savedInstanceState != null) {
60            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
61        }
62    }
63
64    @Override
65    protected void onSaveInstanceState(Bundle outState) {
66        super.onSaveInstanceState(outState);
67        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
68    }
69
70    /**
71     * This is a helper class that implements the management of tabs and all
72     * details of connecting a ViewPager with associated TabHost.  It relies on a
73     * trick.  Normally a tab host has a simple API for supplying a View or
74     * Intent that each tab will show.  This is not sufficient for switching
75     * between pages.  So instead we make the content part of the tab host
76     * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
77     * view to show as the tab content.  It listens to changes in tabs, and takes
78     * care of switch to the correct paged in the ViewPager whenever the selected
79     * tab changes.
80     */
81    public static class TabsAdapter extends FragmentPagerAdapter
82            implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
83        private final Context mContext;
84        private final ActionBar mActionBar;
85        private final ViewPager mViewPager;
86        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
87
88        static final class TabInfo {
89            private final Class<?> clss;
90            private final Bundle args;
91
92            TabInfo(Class<?> _class, Bundle _args) {
93                clss = _class;
94                args = _args;
95            }
96        }
97
98        public TabsAdapter(Activity activity, ViewPager pager) {
99            super(activity.getFragmentManager());
100            mContext = activity;
101            mActionBar = activity.getActionBar();
102            mViewPager = pager;
103            mViewPager.setAdapter(this);
104            mViewPager.addOnPageChangeListener(this);
105        }
106
107        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
108            TabInfo info = new TabInfo(clss, args);
109            tab.setTag(info);
110            tab.setTabListener(this);
111            mTabs.add(info);
112            mActionBar.addTab(tab);
113            notifyDataSetChanged();
114        }
115
116        @Override
117        public int getCount() {
118            return mTabs.size();
119        }
120
121        @Override
122        public Fragment getItem(int position) {
123            TabInfo info = mTabs.get(position);
124            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
125        }
126
127        @Override
128        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
129        }
130
131        @Override
132        public void onPageSelected(int position) {
133            mActionBar.setSelectedNavigationItem(position);
134        }
135
136        @Override
137        public void onPageScrollStateChanged(int state) {
138        }
139
140        @Override
141        public void onTabSelected(Tab tab, FragmentTransaction ft) {
142            Object tag = tab.getTag();
143            for (int i=0; i<mTabs.size(); i++) {
144                if (mTabs.get(i) == tag) {
145                    mViewPager.setCurrentItem(i);
146                }
147            }
148        }
149
150        @Override
151        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
152        }
153
154        @Override
155        public void onTabReselected(Tab tab, FragmentTransaction ft) {
156        }
157    }
158}
159//END_INCLUDE(complete)
160