Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import android.support.v7.widget.LinearLayoutManager;
  2. import android.support.v7.widget.RecyclerView;
  3. import android.support.v7.widget.StaggeredGridLayoutManager;
  4.  
  5. /**
  6. * Created by jihoon on 2016. 8. 11..
  7. */
  8.  
  9. public abstract class OnScrollEndListener extends RecyclerView.OnScrollListener {
  10.  
  11. public static int REVERSE = 1;
  12.  
  13.  
  14. private boolean isArray;
  15. private boolean reverse;
  16. private StaggeredGridLayoutManager staggeredGridLayoutManager;
  17. private LinearLayoutManager linearLayoutManager;
  18.  
  19. public OnScrollEndListener(RecyclerView.LayoutManager layoutManager) {
  20. init(layoutManager);
  21. }
  22.  
  23. public OnScrollEndListener(RecyclerView.LayoutManager layoutManager, int direction) {
  24. this.reverse = direction == REVERSE;
  25. init(layoutManager);
  26. }
  27.  
  28. private void init(RecyclerView.LayoutManager layoutManager) {
  29. isArray = layoutManager instanceof StaggeredGridLayoutManager;
  30. if (isArray)
  31. staggeredGridLayoutManager = ((StaggeredGridLayoutManager) layoutManager);
  32. else
  33. linearLayoutManager = ((LinearLayoutManager) layoutManager);
  34. }
  35.  
  36. @Override
  37. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  38. super.onScrolled(recyclerView, dx, dy);
  39.  
  40.  
  41. if (isArray) {
  42.  
  43. int[] positions =
  44. reverse ? staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null)
  45. : staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(null);
  46.  
  47. // positions array length equals spancount
  48.  
  49. int lastPosition = staggeredGridLayoutManager.getItemCount() - 1;
  50.  
  51. for (int position : positions) {
  52. callbackIfLastPosition(lastPosition, position);
  53. }
  54.  
  55. } else {
  56. // LinearLayoutManager is super class of GridLayoutManager .
  57. int position =
  58. reverse ? linearLayoutManager.findFirstCompletelyVisibleItemPosition()
  59. : linearLayoutManager.findLastCompletelyVisibleItemPosition();
  60.  
  61. int lastPosition = linearLayoutManager.getItemCount() - 1; // linearLayoutManager.getItemCount() - 1 - preLoadCount
  62.  
  63. callbackIfLastPosition(lastPosition, position);
  64. }
  65. }
  66.  
  67. private void callbackIfLastPosition(int lastPosition, int position) {
  68. if (position == lastPosition) {
  69. loadMore(position);
  70. }
  71. }
  72.  
  73. public abstract void loadMore(int position);
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement