ncosentino

Tabs Fragment Tutorial - Classes - TabFragment.java

Oct 30th, 2013
1,509
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Blog Post:
  3.  * http://devleader.ca/2013/11/04/fragments-tabbed-android-user-interface
  4.  *
  5.  * Layouts:
  6.  * activity_main.xml: http://pastebin.com/kj8M38Ze
  7.  * fragment_tabs.xml: http://pastebin.com/0b7HAeAK
  8.  * simple_tab.xml: http://pastebin.com/9SqLbS0X
  9.  *
  10.  * Classes:
  11.  * MainActivity.java: http://pastebin.com/eBv3H775
  12.  * SimpleTabDefinition.java: http://pastebin.com/wW3WCcy9
  13.  * TabDefinition.java: http://pastebin.com/KSRV2f3U
  14.  * TabsFragment.java: http://pastebin.com/9Asv7AVH
  15.  */
  16. package com.devleader.tab_fragment_tutorial;
  17.  
  18. import android.os.Bundle;
  19. import android.support.v4.app.Fragment;
  20. import android.support.v4.app.FragmentManager;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.view.ViewGroup;
  24. import android.widget.TabHost;
  25. import android.widget.TabHost.OnTabChangeListener;
  26. import android.widget.TabHost.TabSpec;
  27.  
  28. /**
  29.  * A {@link Fragment} used to switch between tabs.
  30.  */
  31. public class TabsFragment extends Fragment implements OnTabChangeListener {
  32.     //
  33.     // Constants
  34.     //
  35.     private final TabDefinition[] TAB_DEFINITIONS = new TabDefinition[] {
  36.         new SimpleTabDefinition(R.id.tab1, R.layout.simple_tab, R.string.tab_title_1, R.id.tabTitle, new Fragment()),
  37.         new SimpleTabDefinition(R.id.tab2, R.layout.simple_tab, R.string.tab_title_2, R.id.tabTitle, new Fragment()),
  38.     };
  39.    
  40.     //
  41.     // Fields
  42.     //
  43.     private View _viewRoot;
  44.     private TabHost _tabHost;
  45.        
  46.     //
  47.     // Exposed Members
  48.     //
  49.     @Override
  50.     public void onTabChanged(String tabId) {
  51.         for (TabDefinition tab : TAB_DEFINITIONS) {
  52.             if (tabId != tab.getId()) {
  53.                 continue;
  54.             }
  55.            
  56.             updateTab(tabId, tab.getFragment(), tab.getTabContentViewId());
  57.             return;
  58.         }
  59.        
  60.         throw new IllegalArgumentException("The specified tab id '" + tabId + "' does not exist.");
  61.     }
  62.    
  63.     @Override
  64.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  65.         _viewRoot = inflater.inflate(R.layout.fragment_tabs, null);
  66.        
  67.         _tabHost = (TabHost)_viewRoot.findViewById(android.R.id.tabhost);
  68.         _tabHost.setup();
  69.        
  70.         for (TabDefinition tab : TAB_DEFINITIONS) {
  71.             _tabHost.addTab(createTab(inflater, _tabHost, _viewRoot, tab));
  72.         }
  73.        
  74.         return _viewRoot;
  75.     }
  76.    
  77.     @Override
  78.     public void onActivityCreated(Bundle savedInstanceState) {
  79.         super.onActivityCreated(savedInstanceState);
  80.         setRetainInstance(true);
  81.  
  82.         _tabHost.setOnTabChangedListener(this);
  83.        
  84.         if (TAB_DEFINITIONS.length > 0) {
  85.             onTabChanged(TAB_DEFINITIONS[0].getId());
  86.         }
  87.     }
  88.    
  89.     //
  90.     // Internal Members
  91.     //
  92.     /**
  93.      * Creates a {@link TabSpec} based on the specified parameters.
  94.      * @param inflater The {@link LayoutInflater} responsible for creating {@link View}s.
  95.      * @param tabHost The {@link TabHost} used to create new {@link TabSpec}s.
  96.      * @param root The root {@link View} for the {@link Fragment}.
  97.      * @param tabDefinition The {@link TabDefinition} that defines what the tab will look and act like.
  98.      * @return A new {@link TabSpec} instance.
  99.      */
  100.     private TabSpec createTab(LayoutInflater inflater, TabHost tabHost, View root, TabDefinition tabDefinition) {
  101.         ViewGroup tabsView = (ViewGroup)root.findViewById(android.R.id.tabs);
  102.         View tabView = tabDefinition.createTabView(inflater, tabsView);
  103.          
  104.         TabSpec tabSpec = tabHost.newTabSpec(tabDefinition.getId());
  105.         tabSpec.setIndicator(tabView);
  106.         tabSpec.setContent(tabDefinition.getTabContentViewId());
  107.         return tabSpec;
  108.     }
  109.    
  110.     /**
  111.      * Called when switching between tabs.
  112.      * @param tabId The unique identifier for the tab.
  113.      * @param fragment The {@link Fragment} to swap in for the tab.
  114.      * @param containerId The layout ID for the {@link View} that houses the tab's content.
  115.      */
  116.     private void updateTab(String tabId, Fragment fragment, int containerId) {
  117.         final FragmentManager manager = getFragmentManager();
  118.         if (manager.findFragmentByTag(tabId) == null) {
  119.             manager.beginTransaction()
  120.                 .replace(containerId, fragment, tabId)
  121.                 .commit();
  122.         }
  123.     }
  124. }
RAW Paste Data