blob: 24900476cbb1c546848e84d0751990bc11e86780 [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 {
Pankaj Garg68faf1c2015-06-26 17:07:37 -070044 private final Context mContext;
45 private final ActionBar mActionBar;
46 private final ViewPager mViewPager;
47 private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
Tarun Nainani8dc931d2015-04-30 06:46:28 -070048
Pankaj Garg68faf1c2015-06-26 17:07:37 -070049 static final class TabInfo {
50 private final Class<?> clss;
51 private final Bundle args;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070052
Pankaj Garg68faf1c2015-06-26 17:07:37 -070053 TabInfo(Class<?> _class, Bundle _args) {
54 clss = _class;
55 args = _args;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070056 }
Pankaj Garg68faf1c2015-06-26 17:07:37 -070057 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070058
Pankaj Garg68faf1c2015-06-26 17:07:37 -070059 public ComboTabsAdapter(Activity activity, ViewPager pager) {
60 super(activity.getFragmentManager());
61 mContext = activity;
62 mActionBar = activity.getActionBar();
63 mViewPager = pager;
64 mViewPager.setAdapter(this);
65 mViewPager.setOnPageChangeListener(this);
66 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070067
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
Pankaj Garg68faf1c2015-06-26 17:07:37 -070077 public void removeAllTabs() {
78 mActionBar.removeAllTabs();
79 notifyDataSetChanged();
80 }
81
Tarun Nainani87a86682015-02-05 11:47:35 -080082 @Override
83 public int getCount() {
84 return mTabs.size();
85 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070086
Tarun Nainani87a86682015-02-05 11:47:35 -080087 @Override
88 public Fragment getItem(int position) {
89 TabInfo info = mTabs.get(position);
90 return Fragment.instantiate(mContext, info.clss.getName(), info.args);
91 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070092
Tarun Nainani87a86682015-02-05 11:47:35 -080093 @Override
94 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
95 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -070096
Tarun Nainani87a86682015-02-05 11:47:35 -080097 @Override
98 public void onPageSelected(int position) {
99 mActionBar.setSelectedNavigationItem(position);
100 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700101
Tarun Nainani87a86682015-02-05 11:47:35 -0800102 @Override
103 public void onPageScrollStateChanged(int state) {
104 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700105
Tarun Nainani87a86682015-02-05 11:47:35 -0800106 @Override
107 public void onTabSelected(android.app.ActionBar.Tab tab,
108 FragmentTransaction ft) {
109 Object tag = tab.getTag();
110 for (int i=0; i<mTabs.size(); i++) {
111 if (mTabs.get(i) == tag) {
112 mViewPager.setCurrentItem(i);
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700113 }
114 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700115 }
116
Tarun Nainani87a86682015-02-05 11:47:35 -0800117 @Override
118 public void onTabUnselected(android.app.ActionBar.Tab tab,
119 FragmentTransaction ft) {
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700120 }
121
Tarun Nainani87a86682015-02-05 11:47:35 -0800122 @Override
123 public void onTabReselected(android.app.ActionBar.Tab tab,
124 FragmentTransaction ft) {
125 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700126}
Tarun Nainani87a86682015-02-05 11:47:35 -0800127