Advertisement
Guest User

Untitled

a guest
Dec 11th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. package goodapps1.scarystories;
  2.  
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.app.FragmentPagerAdapter;
  7. import android.support.v4.app.FragmentTransaction;
  8. import android.support.v4.view.ViewPager;
  9. import android.view.View;
  10. import android.widget.TabHost;
  11.  
  12. import com.actionbarsherlock.app.ActionBar;
  13. import com.actionbarsherlock.app.ActionBar.Tab;
  14. import com.actionbarsherlock.app.SherlockFragmentActivity;
  15.  
  16. import java.util.ArrayList;
  17.  
  18. public class FragmentTabsPager extends SherlockFragmentActivity {
  19.     ViewPager  mViewPager;
  20.     TabsAdapter mTabsAdapter;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.  
  26.         setContentView(R.layout.fragment_tabs_pager);
  27.  
  28.         mViewPager = (ViewPager)findViewById(R.id.pager);
  29.  
  30.         // This block thanks to http://stackoverflow.com/q/9790279/517561
  31.         ActionBar bar = getSupportActionBar();
  32.         bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  33.         bar.setDisplayShowTitleEnabled(false);
  34.         bar.setDisplayShowHomeEnabled(false);
  35.         //
  36.  
  37.         StoriesManager.Init(this);
  38.  
  39.         mTabsAdapter = new TabsAdapter(this, mViewPager);
  40.  
  41.         Bundle args1 = new Bundle();
  42.         args1.putInt("type", 1);
  43.  
  44.         Bundle args2 = new Bundle();
  45.         args2.putInt("type", 2);
  46.  
  47.         Bundle args3 = new Bundle();
  48.         args3.putInt("type", 3);
  49.  
  50.         mTabsAdapter.addTab("CATEGORIES", "CATEGORIES",
  51.                 CategoriesActivity.CountingFragment.class, args1);
  52.         mTabsAdapter.addTab("ALL", "ALL",
  53.                 StoriesList.AppListFragment.class, args2);
  54.         mTabsAdapter.addTab("FAVOURITES", "FAVOURITES",
  55.                 StoriesList.AppListFragment.class, args3);
  56.  
  57.     }
  58.  
  59.     /**
  60.      * This is a helper class that implements the management of tabs and all
  61.      * details of connecting a ViewPager with associated TabHost.  It relies on a
  62.      * trick.  Normally a tab host has a simple API for supplying a View or
  63.      * Intent that each tab will show.  This is not sufficient for switching
  64.      * between pages.  So instead we make the content part of the tab host
  65.      * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
  66.      * view to show as the tab content.  It listens to changes in tabs, and takes
  67.      * care of switch to the correct paged in the ViewPager whenever the selected
  68.      * tab changes.
  69.      */
  70.     public static class TabsAdapter extends FragmentPagerAdapter
  71.             implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
  72.         private final SherlockFragmentActivity mContext;
  73.         private final ViewPager mViewPager;
  74.         private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
  75.  
  76.         static final class TabInfo {
  77.             @SuppressWarnings("unused")
  78.             private final String tag;
  79.             private final Class<?> clss;
  80.             private final Bundle args;
  81.  
  82.             TabInfo(String _tag, Class<?> _class, Bundle _args) {
  83.                 tag = _tag;
  84.                 clss = _class;
  85.                 args = _args;
  86.             }
  87.         }
  88.  
  89.         static class DummyTabFactory implements TabHost.TabContentFactory {
  90.             private final Context mContext;
  91.  
  92.             public DummyTabFactory(Context context) {
  93.                 mContext = context;
  94.             }
  95.  
  96.             @Override
  97.             public View createTabContent(String tag) {
  98.                 View v = new View(mContext);
  99.                 v.setMinimumWidth(0);
  100.                 v.setMinimumHeight(0);
  101.                 return v;
  102.             }
  103.         }
  104.  
  105.         public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
  106.             super(activity.getSupportFragmentManager());
  107.             mContext = activity;
  108.             mViewPager = pager;
  109.             mViewPager.setAdapter(this);
  110.             mViewPager.setOnPageChangeListener(this);
  111.         }
  112.        
  113.         public void addTab(String tag, CharSequence label, Class<?> clss, Bundle args) {
  114.             Tab tab = mContext.getSupportActionBar().newTab();
  115.             tab.setText(label);
  116.             tab.setTabListener(this);
  117.             mContext.getSupportActionBar().addTab(tab);
  118.             TabInfo info = new TabInfo(tag, clss, args);
  119.             mTabs.add(info);
  120.             notifyDataSetChanged();    
  121.         }
  122.        
  123.         @Override
  124.         public int getCount() {
  125.             return mTabs.size();
  126.         }
  127.  
  128.         @Override
  129.         public Fragment getItem(int position) {
  130.             TabInfo info = mTabs.get(position);
  131.             return Fragment.instantiate(mContext, info.clss.getName(), info.args);
  132.         }
  133.  
  134.         @Override
  135.         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  136.         }
  137.  
  138.         @Override
  139.         public void onPageSelected(int position) {
  140.             mContext.getSupportActionBar().setSelectedNavigationItem(position);
  141.         }
  142.  
  143.         @Override
  144.         public void onPageScrollStateChanged(int state) {
  145.         }
  146.  
  147.         /* (non-Javadoc)
  148.          * @see com.actionbarsherlock.app.ActionBar.TabListener#onTabSelected(com.actionbarsherlock.app.ActionBar.Tab, android.support.v4.app.FragmentTransaction)
  149.          */
  150.         @Override
  151.         public void onTabSelected(Tab tab, FragmentTransaction ft) {
  152.           mViewPager.setCurrentItem(mContext.getSupportActionBar().getSelectedNavigationIndex());
  153.         }
  154.  
  155.         /* (non-Javadoc)
  156.          * @see com.actionbarsherlock.app.ActionBar.TabListener#onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab, android.support.v4.app.FragmentTransaction)
  157.          */
  158.         @Override
  159.         public void onTabUnselected(Tab tab, FragmentTransaction ft) {          
  160.         }
  161.  
  162.         /* (non-Javadoc)
  163.          * @see com.actionbarsherlock.app.ActionBar.TabListener#onTabReselected(com.actionbarsherlock.app.ActionBar.Tab, android.support.v4.app.FragmentTransaction)
  164.          */
  165.         @Override
  166.         public void onTabReselected(Tab tab, FragmentTransaction ft) {          
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement