Created
November 30, 2012 15:10
-
-
Save jfmoy/4176306 to your computer and use it in GitHub Desktop.
Tab Manager with ViewPager support.
This file contains 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
package com.orange.labs.dailymotion.kids.activity.managers; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.support.v4.view.ViewPager.OnPageChangeListener; | |
import android.view.View; | |
import android.widget.TabHost; | |
import android.widget.TabHost.TabSpec; | |
/** | |
* Allows to use a {@link TabHost} in combination with a {@link ViewPager}. Each components updates | |
* the other when a changes occur. A new tab can be added using the addTab method, providing a | |
* {@link TabSpec}. Tabs are displayed in order of insertion. | |
*/ | |
public class PagerTabManager implements TabHost.OnTabChangeListener, OnPageChangeListener { | |
private final FragmentActivity mActivity; | |
private final TabHost mTabHost; | |
private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>(); | |
private final ViewPager mViewPager; | |
private final PagerAdapter mViewPagerAdapter; | |
TabInfo mLastTab; | |
static final class TabInfo { | |
private final String tag; | |
private final Class<?> clss; | |
private final Bundle args; | |
private Fragment fragment; | |
TabInfo(String _tag, Class<?> _class, Bundle _args) { | |
tag = _tag; | |
clss = _class; | |
args = _args; | |
} | |
} | |
static class DummyTabFactory implements TabHost.TabContentFactory { | |
private final Context mContext; | |
public DummyTabFactory(Context context) { | |
mContext = context; | |
} | |
@Override | |
public View createTabContent(String tag) { | |
View v = new View(mContext); | |
v.setMinimumWidth(0); | |
v.setMinimumHeight(0); | |
return v; | |
} | |
} | |
/** | |
* Simple Pager Adapter that contains a {@link List} of {@link Fragment}. | |
*/ | |
static class PagerAdapter extends FragmentPagerAdapter { | |
private final List<Fragment> mFragments = new ArrayList<Fragment>(); | |
public PagerAdapter(FragmentManager manager) { | |
super(manager); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return mFragments.get(position); | |
} | |
@Override | |
public int getCount() { | |
return mFragments.size(); | |
} | |
public void addFragment(final Fragment fragment) { | |
mFragments.add(fragment); | |
} | |
} | |
public PagerTabManager(FragmentActivity activity, TabHost tabHost, ViewPager viewPager) { | |
mActivity = activity; | |
mTabHost = tabHost; | |
mViewPagerAdapter = new PagerAdapter(activity.getSupportFragmentManager()); | |
mViewPager = viewPager; | |
mViewPager.setOnPageChangeListener(this); | |
mViewPager.setAdapter(mViewPagerAdapter); | |
mTabHost.setOnTabChangedListener(this); | |
} | |
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) { | |
tabSpec.setContent(new DummyTabFactory(mActivity)); | |
String tag = tabSpec.getTag(); | |
TabInfo info = new TabInfo(tag, clss, args); | |
mViewPagerAdapter.addFragment(Fragment.instantiate(mActivity, info.clss.getName(), args)); | |
mTabs.put(info.tag, info); | |
mTabHost.addTab(tabSpec); | |
} | |
@Override | |
public void onTabChanged(String tabId) { | |
int position = mTabHost.getCurrentTab(); | |
mViewPager.setCurrentItem(position); | |
} | |
@Override | |
public void onPageScrollStateChanged(int arg0) { | |
} | |
@Override | |
public void onPageScrolled(int arg0, float arg1, int arg2) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
mTabHost.setCurrentTab(position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment