Created
February 7, 2014 09:22
-
-
Save myamamic/8859608 to your computer and use it in GitHub Desktop.
[Android] タブ管理クラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.ActionBar; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentTransaction; | |
public class TabManager { | |
public static <T extends Fragment> ActionBar.Tab createTab( | |
Activity activity, ActionBar actionBar, int fragmentContainerId, | |
int text_resid, String tag, Class<T> clazz, boolean select) { | |
ActionBar.Tab tab = actionBar.newTab(); | |
tab.setText(text_resid); | |
tab.setTabListener(new SupportFragmentTabListener<T>( | |
fragmentContainerId, activity, tag, clazz)); | |
actionBar.addTab(tab); | |
if (select) { | |
actionBar.selectTab(tab); | |
} | |
return tab; | |
} | |
public static class SupportFragmentTabListener<T extends Fragment> | |
implements ActionBar.TabListener { | |
private Fragment mFragment; | |
private Activity mActivity; | |
private String mTag; | |
private Class<T> mClass; | |
private int mfragmentContainerId; | |
public SupportFragmentTabListener(int fragmentContainerId, | |
Activity activity, String tag, Class<T> clz) { | |
mActivity = activity; | |
mTag = tag; | |
mClass = clz; | |
mfragmentContainerId = fragmentContainerId; | |
} | |
/* The following are each of the ActionBar.TabListener callbacks */ | |
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { | |
// Check if the fragment is already initialized | |
if (mFragment == null) { | |
// If not, instantiate and add it to the activity | |
mFragment = Fragment.instantiate(mActivity, mClass.getName()); | |
ft.add(mfragmentContainerId, mFragment, mTag); | |
} else { | |
// If it exists, simply attach it in order to show it | |
ft.attach(mFragment); | |
} | |
} | |
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction sft) { | |
if (mFragment != null) { | |
// Detach the fragment, because another one is being attached | |
sft.detach(mFragment); | |
} | |
} | |
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { | |
// User selected the already selected tab. Usually do nothing. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment