blob: f73a88c54923e9fcf4ea65a30304b39cd8e15551 [file] [log] [blame]
Tarun Nainani8dc931d2015-04-30 06:46:28 -07001/*
Tarun Nainani87a86682015-02-05 11:47:35 -08002 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
Tarun Nainani8dc931d2015-04-30 06:46:28 -07003 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Tarun Nainani87a86682015-02-05 11:47:35 -080017
Tarun Nainani8dc931d2015-04-30 06:46:28 -070018package com.android.browser;
19
20import android.app.ActionBar;
21import android.app.Activity;
22import android.app.Fragment;
23import android.app.FragmentTransaction;
24import android.content.Context;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070025import android.os.Bundle;
26import android.support.v13.app.FragmentPagerAdapter;
27import android.support.v4.view.ViewPager;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070028
29import java.util.ArrayList;
30
Tarun Nainani87a86682015-02-05 11:47:35 -080031/**
32 * This is a helper class that implements the management of tabs and all
33 * details of connecting a ViewPager with associated TabHost. It relies on a
34 * trick. Normally a tab host has a simple API for supplying a View or
35 * Intent that each tab will show. This is not sufficient for switching
36 * between pages. So instead we make the content part of the tab host
37 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
38 * view to show as the tab content. It listens to changes in tabs, and takes
39 * care of switch to the correct page in the ViewPager whenever the selected
40 * tab changes.
41 */
42public class ComboTabsAdapter extends FragmentPagerAdapter
43 implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
Tarun Nainani8dc931d2015-04-30 06:46:28 -070044 private final Context mContext;
45 private final ActionBar mActionBar;
46 private final ViewPager mViewPager;
47 private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
48
49 static final class TabInfo {
50 private final Class<?> clss;
51 private final Bundle args;
52
53 TabInfo(Class<?> _class, Bundle _args) {
54 clss = _class;
55 args = _args;
56 }
57 }
58
Tarun Nainani87a86682015-02-05 11:47:35 -080059 public ComboTabsAdapter(Activity activity, ViewPager pager) {
Tarun Nainani8dc931d2015-04-30 06:46:28 -070060 super(activity.getFragmentManager());
61 mContext = activity;
62 mActionBar = activity.getActionBar();
63 mViewPager = pager;
64 mViewPager.setAdapter(this);
65 mViewPager.setOnPageChangeListener(this);
66 }
67
Tarun Nainani87a86682015-02-05 11:47:35 -080068 public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
69 TabInfo info = new TabInfo(clss, args);
70 tab.setTag(info);
71 tab.setTabListener(this);
72 mTabs.add(info);
73 mActionBar.addTab(tab);
74 notifyDataSetChanged();
75 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070076
Tarun Nainani87a86682015-02-05 11:47:35 -080077 @Override
78 public int getCount() {
79 return mTabs.size();
80 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070081
Tarun Nainani87a86682015-02-05 11:47:35 -080082 @Override
83 public Fragment getItem(int position) {
84 TabInfo info = mTabs.get(position);
85 return Fragment.instantiate(mContext, info.clss.getName(), info.args);
86 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070087
Tarun Nainani87a86682015-02-05 11:47:35 -080088 @Override
89 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
90 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070091
Tarun Nainani87a86682015-02-05 11:47:35 -080092 @Override
93 public void onPageSelected(int position) {
94 mActionBar.setSelectedNavigationItem(position);
95 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070096
Tarun Nainani87a86682015-02-05 11:47:35 -080097 @Override
98 public void onPageScrollStateChanged(int state) {
99 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700100
Tarun Nainani87a86682015-02-05 11:47:35 -0800101 @Override
102 public void onTabSelected(android.app.ActionBar.Tab tab,
103 FragmentTransaction ft) {
104 Object tag = tab.getTag();
105 for (int i=0; i<mTabs.size(); i++) {
106 if (mTabs.get(i) == tag) {
107 mViewPager.setCurrentItem(i);
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700108 }
109 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700110 }
111
Tarun Nainani87a86682015-02-05 11:47:35 -0800112 @Override
113 public void onTabUnselected(android.app.ActionBar.Tab tab,
114 FragmentTransaction ft) {
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700115 }
116
Tarun Nainani87a86682015-02-05 11:47:35 -0800117 @Override
118 public void onTabReselected(android.app.ActionBar.Tab tab,
119 FragmentTransaction ft) {
120 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700121}
Tarun Nainani87a86682015-02-05 11:47:35 -0800122