1/* 2 * Copyright (C) 2011 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/** 33 * This demonstrates the use of action bar tabs and how they interact 34 * with other action bar features. 35 */ 36//BEGIN_INCLUDE(complete) 37public class ActionBarTabsPager extends Activity { 38 ViewPager mViewPager; 39 TabsAdapter mTabsAdapter; 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 mViewPager = new ViewPager(this); 46 mViewPager.setId(R.id.pager); 47 setContentView(mViewPager); 48 49 final ActionBar bar = getActionBar(); 50 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 51 bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 52 53 mTabsAdapter = new TabsAdapter(this, mViewPager); 54 mTabsAdapter.addTab(bar.newTab().setText("Simple"), 55 CountingFragment.class, null); 56 mTabsAdapter.addTab(bar.newTab().setText("List"), 57 FragmentPagerSupport.ArrayListFragment.class, null); 58 mTabsAdapter.addTab(bar.newTab().setText("Cursor"), 59 CursorFragment.class, null); 60 61 if (savedInstanceState != null) { 62 bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0)); 63 } 64 } 65 66 @Override 67 protected void onSaveInstanceState(Bundle outState) { 68 super.onSaveInstanceState(outState); 69 outState.putInt("tab", getActionBar().getSelectedNavigationIndex()); 70 } 71 72 /** 73 * This is a helper class that implements the management of tabs and all 74 * details of connecting a ViewPager with associated TabHost. It relies on a 75 * trick. Normally a tab host has a simple API for supplying a View or 76 * Intent that each tab will show. This is not sufficient for switching 77 * between pages. So instead we make the content part of the tab host 78 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy 79 * view to show as the tab content. It listens to changes in tabs, and takes 80 * care of switch to the correct paged in the ViewPager whenever the selected 81 * tab changes. 82 */ 83 public static class TabsAdapter extends FragmentPagerAdapter 84 implements ActionBar.TabListener, ViewPager.OnPageChangeListener { 85 private final Context mContext; 86 private final ActionBar mActionBar; 87 private final ViewPager mViewPager; 88 private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); 89 90 static final class TabInfo { 91 private final Class<?> clss; 92 private final Bundle args; 93 94 TabInfo(Class<?> _class, Bundle _args) { 95 clss = _class; 96 args = _args; 97 } 98 } 99 100 public TabsAdapter(Activity activity, ViewPager pager) { 101 super(activity.getFragmentManager()); 102 mContext = activity; 103 mActionBar = activity.getActionBar(); 104 mViewPager = pager; 105 mViewPager.setAdapter(this); 106 mViewPager.addOnPageChangeListener(this); 107 } 108 109 public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) { 110 TabInfo info = new TabInfo(clss, args); 111 tab.setTag(info); 112 tab.setTabListener(this); 113 mTabs.add(info); 114 mActionBar.addTab(tab); 115 notifyDataSetChanged(); 116 } 117 118 @Override 119 public int getCount() { 120 return mTabs.size(); 121 } 122 123 @Override 124 public Fragment getItem(int position) { 125 TabInfo info = mTabs.get(position); 126 return Fragment.instantiate(mContext, info.clss.getName(), info.args); 127 } 128 129 @Override 130 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 131 } 132 133 @Override 134 public void onPageSelected(int position) { 135 mActionBar.setSelectedNavigationItem(position); 136 } 137 138 @Override 139 public void onPageScrollStateChanged(int state) { 140 } 141 142 @Override 143 public void onTabSelected(Tab tab, FragmentTransaction ft) { 144 Object tag = tab.getTag(); 145 for (int i=0; i<mTabs.size(); i++) { 146 if (mTabs.get(i) == tag) { 147 mViewPager.setCurrentItem(i); 148 } 149 } 150 } 151 152 @Override 153 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 154 } 155 156 @Override 157 public void onTabReselected(Tab tab, FragmentTransaction ft) { 158 } 159 } 160} 161//END_INCLUDE(complete) 162