1 /* 2 * Copyright 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 17 package com.example.android.tabcompat.lib; 18 19 import android.app.ActionBar; 20 import android.app.ActionBar.Tab; 21 import android.graphics.drawable.Drawable; 22 import android.support.v4.app.Fragment; 23 import android.support.v4.app.FragmentActivity; 24 import android.support.v4.app.FragmentTransaction; 25 26 /** 27 * An implementation of the {@link CompatTab} interface that relies on API 11 APIs. 28 */ 29 public class CompatTabHoneycomb extends CompatTab implements ActionBar.TabListener { 30 31 /** 32 * The native tab object that this {@link CompatTab} acts as a proxy for. 33 */ 34 ActionBar.Tab mTab; 35 CompatTabListener mCallback; 36 Fragment mFragment; 37 CompatTabHoneycomb(FragmentActivity activity, String tag)38 protected CompatTabHoneycomb(FragmentActivity activity, String tag) { 39 super(activity, tag); 40 mTab = activity.getActionBar().newTab(); 41 } 42 43 @Override setText(int resId)44 public CompatTab setText(int resId) { 45 mTab.setText(resId); 46 return this; 47 } 48 49 @Override setIcon(int resId)50 public CompatTab setIcon(int resId) { 51 mTab.setIcon(resId); 52 return this; 53 } 54 55 @Override setTabListener(CompatTabListener callback)56 public CompatTab setTabListener(CompatTabListener callback) { 57 mCallback = callback; 58 mTab.setTabListener(this); 59 return this; 60 } 61 62 @Override getText()63 public CharSequence getText() { 64 return mTab.getText(); 65 } 66 67 @Override getIcon()68 public Drawable getIcon() { 69 return mTab.getIcon(); 70 } 71 72 @Override getTab()73 public Object getTab() { 74 return mTab; 75 } 76 77 @Override getCallback()78 public CompatTabListener getCallback() { 79 return mCallback; 80 } 81 82 @Override onTabReselected(Tab tab, android.app.FragmentTransaction f)83 public void onTabReselected(Tab tab, android.app.FragmentTransaction f) { 84 FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 85 ft.disallowAddToBackStack(); 86 mCallback.onTabReselected(this, ft); 87 ft.commit(); 88 } 89 90 @Override onTabSelected(Tab tab, android.app.FragmentTransaction f)91 public void onTabSelected(Tab tab, android.app.FragmentTransaction f) { 92 FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 93 ft.disallowAddToBackStack(); 94 mCallback.onTabSelected(this, ft); 95 ft.commit(); 96 } 97 98 @Override onTabUnselected(Tab arg0, android.app.FragmentTransaction f)99 public void onTabUnselected(Tab arg0, android.app.FragmentTransaction f) { 100 FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); 101 ft.disallowAddToBackStack(); 102 mCallback.onTabUnselected(this, ft); 103 ft.commit(); 104 } 105 106 @Override setFragment(Fragment fragment)107 public CompatTab setFragment(Fragment fragment) { 108 mFragment = fragment; 109 return this; 110 } 111 112 @Override getFragment()113 public Fragment getFragment() { 114 return mFragment; 115 } 116 } 117