Guest User

Untitled

a guest
Aug 10th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. public class CustomPagerAdapter extends PagerAdapter {
  2.  
  3. private Context mContext;
  4.  
  5. public CustomPagerAdapter(Context context) {
  6. mContext = context;
  7. }
  8.  
  9. @Override
  10. public Object instantiateItem(ViewGroup collection, int position) {
  11.  
  12. BottomNavigationModel modelObject = BottomNavigationModel.values()[position];
  13. LayoutInflater inflater = LayoutInflater.from(mContext);
  14. ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
  15. collection.addView(layout);
  16.  
  17. return layout;
  18. }
  19.  
  20. @Override
  21. public void destroyItem(ViewGroup collection, int position, Object view) {
  22. collection.removeView((View) view);
  23. }
  24.  
  25. @Override
  26. public int getCount() {
  27. return BottomNavigationModel.values().length;
  28. }
  29.  
  30. @Override
  31. public boolean isViewFromObject(View view, Object object) {
  32. return view == object;
  33. }
  34.  
  35. @Override
  36. public CharSequence getPageTitle(int position) {
  37. BottomNavigationModel customPagerEnum = BottomNavigationModel.values()[position];
  38. return mContext.getString(customPagerEnum.getTitleResId());
  39. }
  40.  
  41. }
  42.  
  43. viewPager.setAdapter(new CustomPagerAdapter(this));
  44.  
  45. viewPager.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View view) {
  48. // Not calling this one TODO here
  49. }
  50. });
  51.  
  52. public enum BottomNavigationModel {
  53.  
  54. ONE(R.string.page_one, R.layout.view_row_one),
  55. TWO(R.string.page_two, R.layout.view_row_two),
  56. THREE(R.string.page_three, R.layout.view_row_three);
  57.  
  58. private int mTitleResId;
  59. private int mLayoutResId;
  60.  
  61. BottomNavigationModel(int titleResId, int layoutResId) {
  62. mTitleResId = titleResId;
  63. mLayoutResId = layoutResId;
  64. }
  65.  
  66. public int getTitleResId() {
  67. return mTitleResId;
  68. }
  69.  
  70. public int getLayoutResId() {
  71. return mLayoutResId;
  72. }
  73.  
  74. }
  75.  
  76. @Override
  77. public Fragment getItem(int position) {
  78. switch (position) {
  79. case 0:
  80. return new DescriptionFragment();
  81. case 1:
  82. return new ReviewFragment();
  83. case 3:
  84. return new OrderFragment();
  85. default:
  86. return null;
  87. }
  88. }
Add Comment
Please, Sign In to add comment