Guest User

Untitled

a guest
Apr 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package com.astoria.movieapp;
  2. import android.support.v7.widget.GridLayoutManager;
  3. import android.support.v7.widget.LinearLayoutManager;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.support.v7.widget.StaggeredGridLayoutManager;
  6.  
  7. public abstract class EndlessScrollListener extends RecyclerView.OnScrollListener {
  8.  
  9. private static final int NUMBER_OF_REMAINING_ITEMS = 1;
  10.  
  11. private int visibleThreshold = 0;
  12. private int currentPage = 1;
  13. private boolean loading = false;
  14.  
  15. RecyclerView.LayoutManager mLayoutManager;
  16.  
  17. public EndlessScrollListener(LinearLayoutManager layoutManager) {
  18. this.mLayoutManager = layoutManager;
  19. }
  20.  
  21. public EndlessScrollListener(GridLayoutManager layoutManager) {
  22. this.mLayoutManager = layoutManager;
  23. visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
  24. }
  25.  
  26. public EndlessScrollListener(StaggeredGridLayoutManager layoutManager) {
  27. this.mLayoutManager = layoutManager;
  28. visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
  29. }
  30.  
  31. public int getLastVisibleItem(int[] lastVisibleItemPositions) {
  32. int maxSize = 0;
  33. for (int i = 0; i < lastVisibleItemPositions.length; i++) {
  34. if (i == 0) {
  35. maxSize = lastVisibleItemPositions[i];
  36. }
  37. else if (lastVisibleItemPositions[i] > maxSize) {
  38. maxSize = lastVisibleItemPositions[i];
  39. }
  40. }
  41. return maxSize;
  42. }
  43.  
  44. // This happens many times a second during a scroll, so be wary of the code you place here.
  45. // We are given a few useful parameters to help us work out if we need to load some more data,
  46. // but first we check if we are waiting for the previous load to finish.
  47. @Override
  48. public void onScrolled(RecyclerView view, int dx, int dy) {
  49. int lastVisibleItemPosition = 0;
  50. int totalItemCount = mLayoutManager.getItemCount();
  51.  
  52. if (mLayoutManager instanceof StaggeredGridLayoutManager) {
  53. int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
  54. // get maximum element within the list
  55. lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
  56. } else if (mLayoutManager instanceof GridLayoutManager) {
  57. lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
  58. } else if (mLayoutManager instanceof LinearLayoutManager) {
  59. lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
  60. }
  61.  
  62. if (!loading && lastVisibleItemPosition == totalItemCount - NUMBER_OF_REMAINING_ITEMS) {
  63. currentPage++;
  64. onLoadMore(currentPage, totalItemCount, view);
  65. }
  66. }
  67.  
  68. public void resetState() {
  69. this.currentPage = 1;
  70. setLoading(false);
  71. }
  72.  
  73. public void setLoading(boolean loading) {
  74. this.loading = loading;
  75. }
  76.  
  77. // Defines the process for actually loading more data based on page
  78. public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
  79.  
  80. }
Add Comment
Please, Sign In to add comment