You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractPaginationView.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.luticate.utils.ui.views;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.LinearLayout;
  5. import com.luticate.utils.business.LuPromise;
  6. import java.util.List;
  7. import java.util.Vector;
  8. /**
  9. * Created by robin on 12/8/15.
  10. */
  11. public abstract class AbstractPaginationView extends LinearLayout {
  12. private LuPromise.LuConsumer<Integer> _onPageChanged = null;
  13. private int _page = 0;
  14. private int _maxPage = 1;
  15. private int _contextPagesCount = 2;
  16. public AbstractPaginationView(Context context) {
  17. super(context);
  18. init();
  19. }
  20. public AbstractPaginationView(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. init(attrs);
  23. }
  24. public AbstractPaginationView(Context context, AttributeSet attrs, int defStyleAttr) {
  25. super(context, attrs, defStyleAttr);
  26. init(attrs);
  27. }
  28. private void init(AttributeSet attrs)
  29. {
  30. init();
  31. //_contextPagesCount = attrs.getAttributeIntValue(R.styleable.DefaultPaginationView_contextPagesCount, _contextPagesCount);
  32. }
  33. private void init()
  34. {
  35. }
  36. @Override
  37. protected void onAttachedToWindow() {
  38. super.onAttachedToWindow();
  39. if (isInEditMode())
  40. {
  41. onUpdatePages();
  42. }
  43. }
  44. protected List<Integer> getPages()
  45. {
  46. List<Integer> pages = new Vector<>();
  47. int start = Math.max(0, getPage() - getContextPagesCount());
  48. int end = Math.min(start + getContextPagesCount() + 1, getMaxPage());
  49. for (int i = start; i < end; ++i)
  50. {
  51. pages.add(i);
  52. }
  53. return pages;
  54. }
  55. protected abstract void onUpdatePages();
  56. public int getMaxPage() {
  57. return isInEditMode() ? 50 : _maxPage;
  58. }
  59. public void setMaxPage(int maxPage) {
  60. _maxPage = maxPage;
  61. onUpdatePages();
  62. }
  63. public int getContextPagesCount() {
  64. return _contextPagesCount;
  65. }
  66. public void setContextPagesCount(int contextPagesCount) {
  67. _contextPagesCount = contextPagesCount;
  68. onUpdatePages();
  69. }
  70. public void setPage(int page)
  71. {
  72. _page = page;
  73. onUpdatePages();
  74. }
  75. public int getPage()
  76. {
  77. return isInEditMode() ? 24 : _page;
  78. }
  79. public void setOnPageChanged(LuPromise.LuConsumer<Integer> onPageChanged) {
  80. _onPageChanged = onPageChanged;
  81. }
  82. public void loadPage(int page)
  83. {
  84. if (_onPageChanged != null)
  85. {
  86. _onPageChanged.execute(page);
  87. }
  88. }
  89. public void loadNextPage()
  90. {
  91. int page = getPage();
  92. if (page < getMaxPage() - 1)
  93. {
  94. loadPage(page + 1);
  95. }
  96. }
  97. public void loadPreviousPage()
  98. {
  99. int page = getPage();
  100. if (page > 0)
  101. {
  102. loadPage(page - 1);
  103. }
  104. }
  105. }