korstudio

TabManager for Android (TabHost)

Nov 10th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. /**
  2.      * This is a helper class that implements a generic mechanism for
  3.      * associating fragments with the tabs in a tab host.  It relies on a
  4.      * trick.  Normally a tab host has a simple API for supplying a View or
  5.      * Intent that each tab will show.  This is not sufficient for switching
  6.      * between fragments.  So instead we make the content part of the tab host
  7.      * 0dp high (it is not shown) and the TabManager supplies its own dummy
  8.      * view to show as the tab content.  It listens to changes in tabs, and takes
  9.      * care of switch to the correct fragment shown in a separate content area
  10.      * whenever the selected tab changes.
  11.      */
  12.     public static class TabManager implements TabHost.OnTabChangeListener {
  13.         private final FragmentActivity mActivity;
  14.         private final TabHost mTabHost;
  15.         private final int mContainerId;
  16.         private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
  17.         TabInfo mLastTab;
  18.  
  19.         static final class TabInfo {
  20.             private final String tag;
  21.             private final Class<?> clss;
  22.             private final Bundle args;
  23.             private Fragment fragment;
  24.  
  25.             TabInfo(String _tag, Class<?> _class, Bundle _args) {
  26.                 tag = _tag;
  27.                 clss = _class;
  28.                 args = _args;
  29.             }
  30.            
  31.             public String getTag() {
  32.                 return tag;
  33.             }
  34.             public Fragment getFragment() {
  35.                 return fragment;
  36.             }
  37.         }
  38.  
  39.         static class DummyTabFactory implements TabHost.TabContentFactory {
  40.             private final Context mContext;
  41.  
  42.             public DummyTabFactory(Context context) {
  43.                 mContext = context;
  44.             }
  45.  
  46.             @Override
  47.             public View createTabContent(String tag) {
  48.                 View v = new View(mContext);
  49.                 v.setMinimumWidth(0);
  50.                 v.setMinimumHeight(0);
  51.                 return v;
  52.             }
  53.         }
  54.  
  55.         public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
  56.             mActivity = activity;
  57.             mTabHost = tabHost;
  58.             mContainerId = containerId;
  59.             mTabHost.setOnTabChangedListener(this);
  60.         }
  61.  
  62.         public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
  63.             tabSpec.setContent(new DummyTabFactory(mActivity));
  64.             String tag = tabSpec.getTag();
  65.  
  66.             TabInfo info = new TabInfo(tag, clss, args);
  67.  
  68.             // Check to see if we already have a fragment for this tab, probably
  69.             // from a previously saved state.  If so, deactivate it, because our
  70.             // initial state is that a tab isn't shown.
  71.             info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
  72.             if (info.fragment != null && !info.fragment.isDetached()) {
  73.                 FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
  74.                 ft.detach(info.fragment);
  75.                 ft.commit();
  76.             }
  77.  
  78.             mTabs.put(tag, info);
  79.             mTabHost.addTab(tabSpec);
  80.         }
  81.        
  82.         public TabInfo getCurrentTab() {
  83.             return mLastTab;
  84.         }
  85.         public Fragment getCurrentTabFragment() {
  86.             return mLastTab.fragment;
  87.         }
  88.  
  89.         @Override
  90.         public void onTabChanged(String tabId) {
  91.             TabInfo newTab = mTabs.get(tabId);
  92.             Log.i("TAB","new tab ID => @"+tabId+" ("+newTab+")");
  93.             ((SherlockFragmentActivity) mActivity).getSupportActionBar().setTitle(tabId.substring(tabId.indexOf("_")+1));
  94.             if (mLastTab != newTab) {
  95.                 FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
  96.                 if (mLastTab != null) {
  97.                     if (mLastTab.fragment != null) {
  98.                         ft.detach(mLastTab.fragment);
  99.                        
  100.                     }
  101.                 }
  102.                 if (newTab != null) {
  103.                     if (newTab.fragment == null) {
  104.                         newTab.fragment = Fragment.instantiate(mActivity,
  105.                                 newTab.clss.getName(), newTab.args);
  106.                         ft.add(mContainerId, newTab.fragment, newTab.tag);
  107.                     } else {
  108.                         Log.i("TAB","reattached @"+tabId);
  109.                         ft.attach(newTab.fragment);
  110.                     }
  111.                     GATracker.trackView(tabId);
  112.                     APIService.getInstance().postLog(tabId,0,0);
  113.                 }
  114.  
  115.                 mLastTab = newTab;
  116.                 ft.commit();
  117.                 mActivity.getSupportFragmentManager().executePendingTransactions();
  118.             }
  119.         }
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment