fix comment on 5ca17d67ad5c

This commit is contained in:
Jimmy Shih
2012-04-10 09:57:58 -07:00
parent fedcd33b0b
commit d5389e8c3e
@@ -23,6 +23,8 @@ import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.view.View; import android.view.View;
import android.widget.TabHost; import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import java.util.HashMap; import java.util.HashMap;
@@ -35,96 +37,114 @@ import java.util.HashMap;
* TabManager supplies its own dummy view to show as the tab content. It listens * TabManager supplies its own dummy view to show as the tab content. It listens
* to changes in tabs, and takes care of switch to the correct fragment shown in * to changes in tabs, and takes care of switch to the correct fragment shown in
* a separate content area whenever the selected tab changes. * a separate content area whenever the selected tab changes.
* * <p>
* Copied from the Fragment Tabs example in the API 4+ Support Demos. * Copied from the Fragment Tabs example in the API 4+ Support Demos.
* *
* @author Jimmy Shih * @author Jimmy Shih
*/ */
public class TabManager implements TabHost.OnTabChangeListener { public class TabManager implements TabHost.OnTabChangeListener {
private final FragmentActivity mActivity;
private final TabHost mTabHost;
private final int mContainerId;
private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
TabInfo mLastTab;
static final class TabInfo { private final FragmentActivity fragmentActivity;
private final TabHost tabHost;
private final int containerId;
private final HashMap<String, TabInfo> tabs = new HashMap<String, TabInfo>();
private TabInfo lastTabInfo;
/**
* An object to hold a tab's info.
*
* @author Jimmy Shih
*/
private static final class TabInfo {
private final String tag; private final String tag;
private final Class<?> clss; private final Class<?> clss;
private final Bundle args; private final Bundle bundle;
private Fragment fragment; private Fragment fragment;
TabInfo(String _tag, Class<?> _class, Bundle _args) { public TabInfo(String tag, Class<?> clss, Bundle bundle) {
tag = _tag; this.tag = tag;
clss = _class; this.clss = clss;
args = _args; this.bundle = bundle;
} }
} }
static class DummyTabFactory implements TabHost.TabContentFactory { /**
private final Context mContext; * A dummy {@link TabContentFactory} that creates an empty view to satisfy the
* {@link TabHost} API.
*
* @author Jimmy Shih
*/
private static class DummyTabContentFactory implements TabContentFactory {
public DummyTabFactory(Context context) { private final Context context;
mContext = context;
public DummyTabContentFactory(Context context) {
this.context = context;
} }
@Override @Override
public View createTabContent(String tag) { public View createTabContent(String tag) {
View v = new View(mContext); View view = new View(context);
v.setMinimumWidth(0); view.setMinimumWidth(0);
v.setMinimumHeight(0); view.setMinimumHeight(0);
return v; return view;
} }
} }
public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) { public TabManager(FragmentActivity fragmentActivity, TabHost tabHost, int containerId) {
mActivity = activity; this.fragmentActivity = fragmentActivity;
mTabHost = tabHost; this.tabHost = tabHost;
mContainerId = containerId; this.containerId = containerId;
mTabHost.setOnTabChangedListener(this); tabHost.setOnTabChangedListener(this);
} }
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) { public void addTab(TabSpec tabSpec, Class<?> clss, Bundle bundle) {
tabSpec.setContent(new DummyTabFactory(mActivity)); tabSpec.setContent(new DummyTabContentFactory(fragmentActivity));
String tag = tabSpec.getTag(); String tag = tabSpec.getTag();
TabInfo tabInfo = new TabInfo(tag, clss, bundle);
TabInfo info = new TabInfo(tag, clss, args); /*
* Check to see if we already have a fragment for this tab, probably from a
// Check to see if we already have a fragment for this tab, probably * previously saved state. If so, deactivate it, because our initial state
// from a previously saved state. If so, deactivate it, because our * is that a tab isn't shown.
// initial state is that a tab isn't shown. */
info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag); tabInfo.fragment = fragmentActivity.getSupportFragmentManager().findFragmentByTag(tag);
if (info.fragment != null && !info.fragment.isDetached()) { if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = fragmentActivity.getSupportFragmentManager()
ft.detach(info.fragment); .beginTransaction();
ft.commit(); fragmentTransaction.detach(tabInfo.fragment);
fragmentTransaction.commit();
} }
tabs.put(tag, tabInfo);
mTabs.put(tag, info); tabHost.addTab(tabSpec);
mTabHost.addTab(tabSpec);
} }
@Override @Override
public void onTabChanged(String tabId) { public void onTabChanged(String tabId) {
TabInfo newTab = mTabs.get(tabId); TabInfo newTabInfo = tabs.get(tabId);
if (mLastTab != newTab) { if (lastTabInfo != newTabInfo) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = fragmentActivity.getSupportFragmentManager()
if (mLastTab != null) { .beginTransaction();
if (mLastTab.fragment != null) { if (lastTabInfo != null) {
ft.detach(mLastTab.fragment); if (lastTabInfo.fragment != null) {
fragmentTransaction.detach(lastTabInfo.fragment);
} }
} }
if (newTab != null) { if (newTabInfo != null) {
if (newTab.fragment == null) { if (newTabInfo.fragment == null) {
newTab.fragment = Fragment.instantiate(mActivity, newTab.clss.getName(), newTab.args); newTabInfo.fragment = Fragment.instantiate(
ft.add(mContainerId, newTab.fragment, newTab.tag); fragmentActivity, newTabInfo.clss.getName(), newTabInfo.bundle);
fragmentTransaction.add(containerId, newTabInfo.fragment, newTabInfo.tag);
} else { } else {
ft.attach(newTab.fragment); fragmentTransaction.attach(newTabInfo.fragment);
} }
} }
mLastTab = newTab; lastTabInfo = newTabInfo;
ft.commit(); fragmentTransaction.commit();
mActivity.getSupportFragmentManager().executePendingTransactions(); fragmentActivity.getSupportFragmentManager().executePendingTransactions();
} }
} }
} }