Source code
package android.support.v4.app;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.BaseSavedState;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import java.util.ArrayList;
public class FragmentTabHost extends TabHost implements OnTabChangeListener {
private boolean mAttached;
private int mContainerId;
private Context mContext;
private FragmentManager mFragmentManager;
private TabInfo mLastTab;
private OnTabChangeListener mOnTabChangeListener;
private FrameLayout mRealTabContent;
private final ArrayList<TabInfo> mTabs = new ArrayList();
static class DummyTabFactory implements TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
this.mContext = context;
}
public View createTabContent(String tag) {
View v = new View(this.mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
static class SavedState extends BaseSavedState {
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
String curTab;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
this.curTab = in.readString();
}
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeString(this.curTab);
}
public String toString() {
return "FragmentTabHost.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " curTab=" + this.curTab + "}";
}
}
static final class TabInfo {
private final Bundle args;
private final Class<?> clss;
private Fragment fragment;
private final String tag;
TabInfo(String _tag, Class<?> _class, Bundle _args) {
this.tag = _tag;
this.clss = _class;
this.args = _args;
}
}
public FragmentTabHost(Context context) {
super(context, null);
initFragmentTabHost(context, null);
}
public FragmentTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
initFragmentTabHost(context, attrs);
}
private void initFragmentTabHost(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, new int[]{16842995}, 0, 0);
this.mContainerId = a.getResourceId(0, 0);
a.recycle();
super.setOnTabChangedListener(this);
}
private void ensureHierarchy(Context context) {
if (findViewById(16908307) == null) {
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(1);
addView(ll, new LayoutParams(-1, -1));
TabWidget tw = new TabWidget(context);
tw.setId(16908307);
tw.setOrientation(0);
ll.addView(tw, new LinearLayout.LayoutParams(-1, -2, 0.0f));
FrameLayout fl = new FrameLayout(context);
fl.setId(16908305);
ll.addView(fl, new LinearLayout.LayoutParams(0, 0, 0.0f));
fl = new FrameLayout(context);
this.mRealTabContent = fl;
this.mRealTabContent.setId(this.mContainerId);
ll.addView(fl, new LinearLayout.LayoutParams(-1, 0, 1.0f));
}
}
@Deprecated
public void setup() {
throw new IllegalStateException("Must call setup() that takes a Context and FragmentManager");
}
public void setup(Context context, FragmentManager manager) {
ensureHierarchy(context);
super.setup();
this.mContext = context;
this.mFragmentManager = manager;
ensureContent();
}
public void setup(Context context, FragmentManager manager, int containerId) {
ensureHierarchy(context);
super.setup();
this.mContext = context;
this.mFragmentManager = manager;
this.mContainerId = containerId;
ensureContent();
this.mRealTabContent.setId(containerId);
if (getId() == -1) {
setId(16908306);
}
}
private void ensureContent() {
if (this.mRealTabContent == null) {
this.mRealTabContent = (FrameLayout) findViewById(this.mContainerId);
if (this.mRealTabContent == null) {
throw new IllegalStateException("No tab content FrameLayout found for id " + this.mContainerId);
}
}
}
public void setOnTabChangedListener(OnTabChangeListener l) {
this.mOnTabChangeListener = l;
}
public void addTab(TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(this.mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
if (this.mAttached) {
info.fragment = this.mFragmentManager.findFragmentByTag(tag);
if (!(info.fragment == null || info.fragment.isDetached())) {
FragmentTransaction ft = this.mFragmentManager.beginTransaction();
ft.detach(info.fragment);
ft.commit();
}
}
this.mTabs.add(info);
addTab(tabSpec);
}
protected void onAttachedToWindow() {
super.onAttachedToWindow();
String currentTab = getCurrentTabTag();
FragmentTransaction ft = null;
for (int i = 0; i < this.mTabs.size(); i++) {
TabInfo tab = (TabInfo) this.mTabs.get(i);
tab.fragment = this.mFragmentManager.findFragmentByTag(tab.tag);
if (!(tab.fragment == null || tab.fragment.isDetached())) {
if (tab.tag.equals(currentTab)) {
this.mLastTab = tab;
} else {
if (ft == null) {
ft = this.mFragmentManager.beginTransaction();
}
ft.detach(tab.fragment);
}
}
}
this.mAttached = true;
ft = doTabChanged(currentTab, ft);
if (ft != null) {
ft.commit();
this.mFragmentManager.executePendingTransactions();
}
}
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
this.mAttached = false;
}
protected Parcelable onSaveInstanceState() {
SavedState ss = new SavedState(super.onSaveInstanceState());
ss.curTab = getCurrentTabTag();
return ss;
}
protected void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setCurrentTabByTag(ss.curTab);
}
public void onTabChanged(String tabId) {
if (this.mAttached) {
FragmentTransaction ft = doTabChanged(tabId, null);
if (ft != null) {
ft.commit();
}
}
if (this.mOnTabChangeListener != null) {
this.mOnTabChangeListener.onTabChanged(tabId);
}
}
private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
TabInfo newTab = null;
for (int i = 0; i < this.mTabs.size(); i++) {
TabInfo tab = (TabInfo) this.mTabs.get(i);
if (tab.tag.equals(tabId)) {
newTab = tab;
}
}
if (newTab == null) {
throw new IllegalStateException("No tab known for tag " + tabId);
}
if (this.mLastTab != newTab) {
if (ft == null) {
ft = this.mFragmentManager.beginTransaction();
}
if (!(this.mLastTab == null || this.mLastTab.fragment == null)) {
ft.detach(this.mLastTab.fragment);
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this.mContext, newTab.clss.getName(), newTab.args);
ft.add(this.mContainerId, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
this.mLastTab = newTab;
}
return ft;
}
}