Advertisement
minafaw3

MyPagerAdapter

Nov 30th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package yuvallevy.testrecyclerview;
  2.  
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentManager;
  5. import android.support.v4.app.FragmentStatePagerAdapter;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import yuvallevy.testrecyclerview.Interfaces.Tab1MainFragmentListner;
  11.  
  12. public class MyPagerAdapter extends FragmentStatePagerAdapter {
  13.  
  14. private List<Fragment> fragmentList = new ArrayList<>();
  15. private List<String> fragmentTitleList = new ArrayList<>();
  16. private Fragment mFragmentAtPos0;
  17. private FragmentManager mFragmentManger;
  18.  
  19. static final int NUM_ITEMS = 3;
  20.  
  21. public MyPagerAdapter(FragmentManager fm) {
  22. super(fm);
  23. }
  24.  
  25. @Override
  26. public Fragment getItem(int pos) {
  27.  
  28. if(pos == 0){
  29. if(mFragmentAtPos0 == null){
  30. return mFragmentAtPos0 = Tab1MainView.newInstance();
  31. }else{
  32. return mFragmentAtPos0;
  33. }
  34. }else {
  35. return fragmentList.get(pos);
  36. }
  37.  
  38. }
  39.  
  40. @Override
  41. public int getItemPosition(Object object) {
  42. if(object instanceof CategorySelected){
  43. return POSITION_NONE;
  44. }else if(object instanceof Tab1Fragment){
  45. return POSITION_NONE;
  46. }else {
  47. return POSITION_UNCHANGED;
  48. }
  49.  
  50. }
  51.  
  52. @Override
  53. public int getCount() {
  54. return NUM_ITEMS;
  55. }
  56.  
  57. @Override
  58. public CharSequence getPageTitle(int position) {
  59. return fragmentTitleList.get(position);
  60. }
  61.  
  62. public void addFragment(Fragment fragment, String title){
  63. fragmentList.add(fragment);
  64. fragmentTitleList.add(title);
  65. }
  66.  
  67. public final class Tab1Fragment implements Tab1MainFragmentListner{
  68.  
  69. @Override
  70. public void onSwitchToNextFragment(int pos) {
  71. mFragmentManger.beginTransaction().remove(mFragmentAtPos0).commit();
  72. mFragmentAtPos0 = CategorySelected.newInstance(pos);
  73. notifyDataSetChanged();
  74. }
  75. }
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement