Guest User

Untitled

a guest
May 20th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. import android.app.ActionBar;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.app.FragmentTransaction;
  5.  
  6.  
  7. public  class TabListener<T extends Fragment> implements ActionBar.TabListener {
  8.     private Fragment mFragment;
  9.     private final Activity mActivity;
  10.     private final String mTag;
  11.     private final Class<T> mClass;
  12.  
  13.     /** Constructor used each time a new tab is created.
  14.      * @param activity  The host Activity, used to instantiate the fragment
  15.      * @param tag  The identifier tag for the fragment
  16.      * @param clz  The fragment's Class, used to instantiate the fragment
  17.      */
  18.     public TabListener(Activity activity, String tag, Class<T> clz) {
  19.         mActivity = activity;
  20.         mTag = tag;
  21.         mClass = clz;
  22.     }
  23.  
  24.     /* The following are each of the ActionBar.TabListener callbacks */
  25.  
  26.     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
  27.         // Check if the fragment is already initialized
  28.         if (mFragment == null) {
  29.             // If not, instantiate and add it to the activity
  30.             mFragment = Fragment.instantiate(mActivity, mClass.getName());
  31.             ft.add(android.R.id.content, mFragment, mTag);
  32.         } else {
  33.             // If it exists, simply attach it in order to show it
  34.             ft.attach(mFragment);
  35.         }
  36.     }
  37.  
  38.     public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
  39.         if (mFragment != null) {
  40.             // Detach the fragment, because another one is being attached
  41.             ft.detach(mFragment);
  42.         }
  43.     }
  44.  
  45.     public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
  46.         // User selected the already selected tab. Usually do nothing.
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment