Skip to content

Instantly share code, notes, and snippets.

@s-aska
Created November 5, 2013 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-aska/7321575 to your computer and use it in GitHub Desktop.
Save s-aska/7321575 to your computer and use it in GitHub Desktop.
/**
* タブの切替毎に必要なFragmentを取得するためのAdapterクラス
*/
public static class SectionsPagerAdapter extends FragmentPagerAdapter {
private final Context mContext;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private static final class TabInfo {
private final Class<?> clazz;
private final Bundle args;
private final String tabTitle;
private final Integer id;
/**
* タブ内のActivity、引数を設定する。
*
* @param clazz タブ内のv4.Fragment
* @param args タブ内のv4.Fragmentに対する引数
* @param tabTitle タブに表示するタイトル
*/
TabInfo(Class<?> clazz, Bundle args, String tabTitle, Integer id) {
this.clazz = clazz;
this.args = args;
this.tabTitle = tabTitle;
this.id = id;
}
}
public SectionsPagerAdapter(FragmentActivity context, ViewPager viewPager) {
super(context.getSupportFragmentManager());
viewPager.setAdapter(this);
mContext = context;
mViewPager = viewPager;
}
@Override
public BaseFragment getItem(int position) {
TabInfo info = mTabs.get(position);
return (BaseFragment) Fragment.instantiate(mContext, info.clazz.getName(), info.args);
}
@Override
public long getItemId(int position) {
return mTabs.get(position).id;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public BaseFragment findFragmentByPosition(int position) {
return (BaseFragment) instantiateItem(mViewPager, position);
}
/**
* タブ内に起動するActivity、引数、タイトルを設定する
*
* @param clazz 起動するv4.Fragmentクラス
* @param args v4.Fragmentに対する引数
* @param tabTitle タブのタイトル
*/
public void addTab(Class<?> clazz, Bundle args, String tabTitle, Integer id) {
TabInfo info = new TabInfo(clazz, args, tabTitle, id);
mTabs.add(info);
}
public void removeTab(int position) {
mTabs.remove(position);
}
@Override
public int getCount() {
// タブ数
return mTabs.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment