Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package goodapps1.scarystories;
- import android.content.Context;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentPagerAdapter;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v4.view.ViewPager;
- import android.view.View;
- import android.widget.TabHost;
- import com.actionbarsherlock.app.ActionBar;
- import com.actionbarsherlock.app.ActionBar.Tab;
- import com.actionbarsherlock.app.SherlockFragmentActivity;
- import java.util.ArrayList;
- public class FragmentTabsPager extends SherlockFragmentActivity {
- ViewPager mViewPager;
- TabsAdapter mTabsAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_tabs_pager);
- mViewPager = (ViewPager)findViewById(R.id.pager);
- // This block thanks to http://stackoverflow.com/q/9790279/517561
- ActionBar bar = getSupportActionBar();
- bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- bar.setDisplayShowTitleEnabled(false);
- bar.setDisplayShowHomeEnabled(false);
- //
- StoriesManager.Init(this);
- mTabsAdapter = new TabsAdapter(this, mViewPager);
- Bundle args1 = new Bundle();
- args1.putInt("type", 1);
- Bundle args2 = new Bundle();
- args2.putInt("type", 2);
- Bundle args3 = new Bundle();
- args3.putInt("type", 3);
- mTabsAdapter.addTab("CATEGORIES", "CATEGORIES",
- CategoriesActivity.CountingFragment.class, args1);
- mTabsAdapter.addTab("ALL", "ALL",
- StoriesList.AppListFragment.class, args2);
- mTabsAdapter.addTab("FAVOURITES", "FAVOURITES",
- StoriesList.AppListFragment.class, args3);
- }
- /**
- * This is a helper class that implements the management of tabs and all
- * details of connecting a ViewPager with associated TabHost. It relies on a
- * trick. Normally a tab host has a simple API for supplying a View or
- * Intent that each tab will show. This is not sufficient for switching
- * between pages. So instead we make the content part of the tab host
- * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
- * view to show as the tab content. It listens to changes in tabs, and takes
- * care of switch to the correct paged in the ViewPager whenever the selected
- * tab changes.
- */
- public static class TabsAdapter extends FragmentPagerAdapter
- implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
- private final SherlockFragmentActivity mContext;
- private final ViewPager mViewPager;
- private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
- static final class TabInfo {
- @SuppressWarnings("unused")
- private final String tag;
- private final Class<?> clss;
- private final Bundle args;
- 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;
- }
- }
- public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
- super(activity.getSupportFragmentManager());
- mContext = activity;
- mViewPager = pager;
- mViewPager.setAdapter(this);
- mViewPager.setOnPageChangeListener(this);
- }
- public void addTab(String tag, CharSequence label, Class<?> clss, Bundle args) {
- Tab tab = mContext.getSupportActionBar().newTab();
- tab.setText(label);
- tab.setTabListener(this);
- mContext.getSupportActionBar().addTab(tab);
- TabInfo info = new TabInfo(tag, clss, args);
- mTabs.add(info);
- notifyDataSetChanged();
- }
- @Override
- public int getCount() {
- return mTabs.size();
- }
- @Override
- public Fragment getItem(int position) {
- TabInfo info = mTabs.get(position);
- return Fragment.instantiate(mContext, info.clss.getName(), info.args);
- }
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- mContext.getSupportActionBar().setSelectedNavigationItem(position);
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- /* (non-Javadoc)
- * @see com.actionbarsherlock.app.ActionBar.TabListener#onTabSelected(com.actionbarsherlock.app.ActionBar.Tab, android.support.v4.app.FragmentTransaction)
- */
- @Override
- public void onTabSelected(Tab tab, FragmentTransaction ft) {
- mViewPager.setCurrentItem(mContext.getSupportActionBar().getSelectedNavigationIndex());
- }
- /* (non-Javadoc)
- * @see com.actionbarsherlock.app.ActionBar.TabListener#onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab, android.support.v4.app.FragmentTransaction)
- */
- @Override
- public void onTabUnselected(Tab tab, FragmentTransaction ft) {
- }
- /* (non-Javadoc)
- * @see com.actionbarsherlock.app.ActionBar.TabListener#onTabReselected(com.actionbarsherlock.app.ActionBar.Tab, android.support.v4.app.FragmentTransaction)
- */
- @Override
- public void onTabReselected(Tab tab, FragmentTransaction ft) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement