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