Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 1 | /* |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 3 | * 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 Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 17 | |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 18 | package com.android.browser; |
| 19 | |
| 20 | import android.app.ActionBar; |
| 21 | import android.app.Activity; |
| 22 | import android.app.Fragment; |
| 23 | import android.app.FragmentTransaction; |
| 24 | import android.content.Context; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.support.v13.app.FragmentPagerAdapter; |
| 27 | import android.support.v4.view.ViewPager; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.ArrayList; |
| 30 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 31 | /** |
| 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 | */ |
| 42 | public class ComboTabsAdapter extends FragmentPagerAdapter |
| 43 | implements ActionBar.TabListener, ViewPager.OnPageChangeListener { |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame^] | 44 | private final Context mContext; |
| 45 | private final ActionBar mActionBar; |
| 46 | private final ViewPager mViewPager; |
| 47 | private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 48 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame^] | 49 | static final class TabInfo { |
| 50 | private final Class<?> clss; |
| 51 | private final Bundle args; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 52 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame^] | 53 | TabInfo(Class<?> _class, Bundle _args) { |
| 54 | clss = _class; |
| 55 | args = _args; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 56 | } |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame^] | 57 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 58 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame^] | 59 | 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 Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 67 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 68 | 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 Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 76 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame^] | 77 | public void removeAllTabs() { |
| 78 | mActionBar.removeAllTabs(); |
| 79 | notifyDataSetChanged(); |
| 80 | } |
| 81 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 82 | @Override |
| 83 | public int getCount() { |
| 84 | return mTabs.size(); |
| 85 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 86 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 87 | @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 Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 92 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 93 | @Override |
| 94 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { |
| 95 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 96 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 97 | @Override |
| 98 | public void onPageSelected(int position) { |
| 99 | mActionBar.setSelectedNavigationItem(position); |
| 100 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 101 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 102 | @Override |
| 103 | public void onPageScrollStateChanged(int state) { |
| 104 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 105 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 106 | @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 Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 117 | @Override |
| 118 | public void onTabUnselected(android.app.ActionBar.Tab tab, |
| 119 | FragmentTransaction ft) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 122 | @Override |
| 123 | public void onTabReselected(android.app.ActionBar.Tab tab, |
| 124 | FragmentTransaction ft) { |
| 125 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 126 | } |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 127 | |