title: Pagination with staggeredGridLayoutManager and recyclerView
body: <p>Hello I need to make the infinite scrolling (Pagination) with the recyclerview using the staggeredGridLayoutManager. Pagination is working but the problem is that <code>onLoadMore()</code> function is called so many times while scrolling which causing problems, here is my code:</p>
  
  <pre><code> newSearchAdapter = new NewSearchAdapter(getActivity(), gridData);
              StaggeredGridLayoutManager mLayoutManager;
              mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
              mLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
              rv_NewProfilesGrid.setLayoutManager(mLayoutManager);
              rv_NewProfilesGrid.setAdapter(newSearchAdapter);
              rv_NewProfilesGrid.addOnScrollListener(new EndlessRecyclerOnScrollListenerStaggeredLayoutmanager(mLayoutManager) {
                  @Override
                  public void onLoadMore(int current_page) {
  
                    //calling the api
  
                  }
              });
  </code></pre>
  
  <p><strong>and here is my scroll listner</strong> </p>
  
  <p>I think the problem here is with the <code>getFirstVisibleItems()</code> function because with <strong>GridLayoutManger</strong> or <strong>LinearLayoutManager</strong> it returns an integer but with <strong>StaggeredLayout</strong> it returns an int Array, so I did the following: </p>
  
  <pre><code>public abstract class EndlessRecyclerOnScrollListenerStaggeredLayoutmanager extends RecyclerView.OnScrollListener {
      public static String TAG = EndlessRecyclerOnScrollListenerStaggeredLayoutmanager.class.getSimpleName();
      private int scrolledDistance = 0;
      private boolean controlsVisible = false;
  
      private boolean loading = true; // True if we are still waiting for the last set of data to load.
      private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
      public static boolean  loadOneTime = false;
      private int pastVisibleItems, visibleItemCount, totalItemCount,previousTotal;
  
      private int current_page = 1;
  
      private StaggeredGridLayoutManager mStaggeredGridLayoutManager;
  
      public EndlessRecyclerOnScrollListenerStaggeredLayoutmanager(StaggeredGridLayoutManager staggeredGridLayoutManager) {
          this.mStaggeredGridLayoutManager = staggeredGridLayoutManager;
  
      }
  
  
      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
          super.onScrolled(recyclerView, dx, dy);
  
          visibleItemCount = recyclerView.getChildCount();
          totalItemCount = mStaggeredGridLayoutManager.getItemCount();
  
          int[] firstVisibleItems = null;
          firstVisibleItems = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
          if (firstVisibleItems != null &amp;&amp; firstVisibleItems.length &gt; 0) {
              pastVisibleItems = firstVisibleItems[0];
          }
  
  
          if (loading) {
              if ((visibleItemCount + pastVisibleItems) &gt;= totalItemCount) {
                  loading = false;
  
                  previousTotal = totalItemCount;
              }
          }
          if (!loading &amp;&amp; (totalItemCount - visibleItemCount)
                  &lt;= (pastVisibleItems + visibleThreshold)) {
              // End has been reached
  
              // Do something
              current_page++;
              loadOneTime=false;
              onLoadMore(current_page);
  
              loading = true;
          }
  
          if (scrolledDistance &gt; 1 &amp;&amp; controlsVisible) {
              controlsVisible = false;
              scrolledDistance = 0;
          } else if (scrolledDistance &lt; -1 &amp;&amp; !controlsVisible) {
              controlsVisible = true;
              scrolledDistance = 0;
          }
  
          if ((controlsVisible &amp;&amp; dy &gt; 0) || (!controlsVisible &amp;&amp; dy &lt; 0)) {
              scrolledDistance += dy;
          }
      }
  
      public abstract void onLoadMore(int current_page);
  
  
  }
  </code></pre>