Guest User

Untitled

a guest
Jan 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. public class TabsAdapter extends FragmentPagerAdapter implements
  2. ActionBar.TabListener, ViewPager.OnPageChangeListener {
  3. private final Context mContext;
  4. private final ActionBar mActionBar;
  5. private final MyViewPager mViewPager;
  6. private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
  7. public int curItem=0;
  8.  
  9. final class TabInfo {
  10. private final Class<?> clss;
  11. private final Bundle args;
  12.  
  13. TabInfo(Class<?> _class, Bundle _args) {
  14. clss = _class;
  15. args = _args;
  16. }
  17. }
  18.  
  19. public TabsAdapter(SherlockFragmentActivity activity, MyViewPager pager) {
  20. super(activity.getSupportFragmentManager());
  21. mContext = activity;
  22. mActionBar = activity.getSupportActionBar();
  23. mViewPager = pager;
  24. mViewPager.setAdapter(this);
  25. mViewPager.setOnPageChangeListener(this);
  26. }
  27.  
  28. public void addTab(Tab tab, Class<?> clss, Bundle args) {
  29. TabInfo info = new TabInfo(clss, args);
  30. tab.setTag(info);
  31. tab.setTabListener(this);
  32. mTabs.add(info);
  33. mActionBar.addTab(tab);
  34. notifyDataSetChanged();
  35. }
  36.  
  37. @Override
  38. public int getCount() {
  39. return mTabs.size();
  40. }
  41.  
  42. @Override
  43. public Fragment getItem(int position) {
  44. TabInfo info = mTabs.get(position);
  45. Fragment ret = Fragment.instantiate(mContext, info.clss.getName(), info.args);
  46. return ret;
  47. }
  48.  
  49. @Override
  50. public void onPageScrolled(int position, float positionOffset,
  51. int positionOffsetPixels) {
  52. }
  53.  
  54. @Override
  55. public void onPageSelected(int position) {
  56. curItem = position;
  57. Log.e("calc", "onPageSelected");
  58. mActionBar.setSelectedNavigationItem(position);
  59. mViewPager.setSwipeEnabled(position != 2);
  60. }
  61.  
  62. @Override
  63. public void onPageScrollStateChanged(int state) {
  64. }
  65.  
  66. @Override
  67. public void onTabSelected(Tab tab, FragmentTransaction ft) {
  68. Object tag = tab.getTag();
  69. for (int i = 0; i < mTabs.size(); i++) {
  70. if (mTabs.get(i) == tag) {
  71. Log.e("calc", "onTabSelected");
  72. mViewPager.setCurrentItem(i);
  73. mViewPager.setSwipeEnabled(i != 2);
  74. }
  75. }
  76. }
  77.  
  78. @Override
  79. public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  80. }
  81.  
  82. @Override
  83. public void onTabReselected(Tab tab, FragmentTransaction ft) {
  84. }
  85. }
Add Comment
Please, Sign In to add comment