content: package com.alelak.materialupsample;
  
  import android.support.v7.widget.GridLayoutManager;
  import android.support.v7.widget.RecyclerView;
  
  public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
      public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
  
      private int previousTotal = 0;
      private boolean loading = true;
      private int visibleThreshold = 5;
      int firstVisibleItem, visibleItemCount, totalItemCount;
  
      private int current_page = 1;
  
      private GridLayoutManager mGridLayoutManager;
  
      public EndlessRecyclerOnScrollListener(GridLayoutManager gridLayoutManager) {
          this.mGridLayoutManager = gridLayoutManager;
      }
  
      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
          super.onScrolled(recyclerView, dx, dy);
  
          visibleItemCount = recyclerView.getChildCount();
          totalItemCount = mGridLayoutManager.getItemCount();
          firstVisibleItem = mGridLayoutManager.findFirstVisibleItemPosition();
  
          if (loading) {
              if (totalItemCount > previousTotal) {
                  loading = false;
                  previousTotal = totalItemCount;
              }
          }
          if (!loading && (totalItemCount - visibleItemCount)
                  <= (firstVisibleItem + visibleThreshold)) {
              current_page++;
  
              onLoadMore(current_page);
  
              loading = true;
          }
      }
  
      public abstract void onLoadMore(int current_page);
  }