2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.altusmetrum.AltosDroid;
19 import java.util.ArrayList;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.support.v4.app.Fragment;
24 import android.support.v4.app.FragmentTransaction;
25 import android.support.v4.app.FragmentActivity;
26 import android.support.v4.app.FragmentPagerAdapter;
27 import android.support.v4.view.ViewPager;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.TabHost;
31 import android.widget.TabWidget;
34 * This is a helper class that implements the management of tabs and all
35 * details of connecting a ViewPager with associated TabHost. It relies on a
36 * trick. Normally a tab host has a simple API for supplying a View or
37 * Intent that each tab will show. This is not sufficient for switching
38 * between pages. So instead we make the content part of the tab host
39 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
40 * view to show as the tab content. It listens to changes in tabs, and takes
41 * care of switch to the correct paged in the ViewPager whenever the selected
44 public class TabsAdapter extends FragmentPagerAdapter
45 implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
46 private final Context mContext;
47 private final TabHost mTabHost;
48 private final ViewPager mViewPager;
49 private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
52 static class TabInfo {
53 private final String tag;
54 private final Class<?> clss;
55 private final Bundle args;
56 private Fragment fragment;
58 TabInfo(String _tag, Class<?> _class, Bundle _args) {
65 static class DummyTabFactory implements TabHost.TabContentFactory {
66 private final Context mContext;
68 public DummyTabFactory(Context context) {
72 public View createTabContent(String tag) {
73 View v = new View(mContext);
75 v.setMinimumHeight(0);
80 public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
81 super(activity.getSupportFragmentManager());
85 mTabHost.setOnTabChangedListener(this);
86 mViewPager.setAdapter(this);
87 mViewPager.setOnPageChangeListener(this);
90 public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
91 tabSpec.setContent(new DummyTabFactory(mContext));
92 String tag = tabSpec.getTag();
94 TabInfo info = new TabInfo(tag, clss, args);
96 mTabHost.addTab(tabSpec);
97 notifyDataSetChanged();
101 public int getCount() {
106 public Fragment getItem(int position) {
107 TabInfo info = mTabs.get(position);
108 AltosDebug.debug("TabsAdapter.getItem(%d)", position);
109 info.fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
110 return info.fragment;
113 public Fragment currentItem() {
114 TabInfo info = mTabs.get(position);
115 return info.fragment;
118 public void onTabChanged(String tabId) {
119 AltosDroidTab prev_frag = (AltosDroidTab) mTabs.get(position).fragment;
121 position = mTabHost.getCurrentTab();
123 AltosDroidTab cur_frag = (AltosDroidTab) mTabs.get(position).fragment;
125 if (prev_frag != cur_frag) {
126 if (prev_frag != null) {
127 prev_frag.set_visible(false);
130 if (cur_frag != null) {
131 cur_frag.set_visible(true);
133 AltosDebug.debug("TabsAdapter.onTabChanged(%s) = %d", tabId, position);
134 mViewPager.setCurrentItem(position);
137 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
140 public void onPageSelected(int position) {
141 // Unfortunately when TabHost changes the current tab, it kindly
142 // also takes care of putting focus on it when not in touch mode.
144 // This hack tries to prevent this from pulling focus out of our
146 TabWidget widget = mTabHost.getTabWidget();
147 int oldFocusability = widget.getDescendantFocusability();
148 widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
149 mTabHost.setCurrentTab(position);
150 widget.setDescendantFocusability(oldFocusability);
153 public void onPageScrollStateChanged(int state) {