title: Recyclerview StaggeredGridLayoutManager - Find last visible item
body: <p>I am using a Recyclerview with StaggeredGridLayoutManager with Endless Scroll. I want to do a network call when the last item of the list is visible to the user. So here is my code:</p>
  
  <pre><code>   public RecyclerViewAdapter(Context context) {
  
              Log.d(Const.DEBUG, "RecyclerViewAdapter Constructor()");
  
              this.context = context;
  
              final StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
  
              recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                  @Override
                  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                      super.onScrolled(recyclerView, dx, dy);
  
                      totalItemCount = staggeredGridLayoutManager.getItemCount();
                      lastVisibleItems = staggeredGridLayoutManager.findLastVisibleItemPositions(new int[mStaggeredLayoutManager.getSpanCount()]);
  
                      Log.d(Const.DEBUG, "LastVisibleItems: " + Arrays.toString(lastVisibleItems));
                      Log.d(Const.DEBUG, "LastVisibleItems Count: " + lastVisibleItems.length);
  
                      if (staggeredGridLayoutManager.getSpanCount() == 1) {
                          lastVisibleItem = lastVisibleItems[0];
                      } else if (staggeredGridLayoutManager.getSpanCount() == 2) {
                          lastVisibleItem = Math.max(lastVisibleItems[0], lastVisibleItems[1]);
                      } else if (staggeredGridLayoutManager.getSpanCount() == 3) {
                          lastVisibleItem = Math.max(Math.max(lastVisibleItems[0], lastVisibleItems[1]), lastVisibleItems[2]);
                      }
  
  
                      if (!isRefreshing &amp;&amp; (totalItemCount &lt;= lastVisibleItem + visibleThreshold)) {
  
                          Log.d(Const.DEBUG, "isRefreshing: " + isRefreshing);
                          Log.d(Const.DEBUG, "totalItemCount: " + totalItemCount);
                          Log.d(Const.DEBUG, "lastVisibileItem: " + lastVisibleItem);
                          Log.d(Const.DEBUG, "visibileThreshold: " + visibleThreshold);
  
                          Log.d(Const.DEBUG, "calling LoadMore()");
  
                          if (mIOnLoadMoreListener != null) {
                              mIOnLoadMoreListener.onLoadMore();
                          }
  
                          isRefreshing = true;
                      }
                  }
              });
  
          }
  </code></pre>
  
  <p>I get 9 values from server on first call. So, when i move to the last index[8], the lastVisibleItem will be 8 and visibleThreshold is 1, totalItemCount is 9 and so the next network call should happen. What is actually happening now, is, when the screen loads for the first time, the lastVisibleItem should be 1 or 2, but its returning 8, and as a result, the loadMore() is getting called. </p>
  
  <p>Logcat:</p>
  
  <pre><code>D/xx: Items Count: 9
  D/xx: LastVisibleItems: [8]
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: LastVisibleItems Count: 1
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: isRefreshing: false
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: totalItemCount: 9
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: lastVisibileItem: 8
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: visibileThershold: 1
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: calling LoadMore()
  03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: onLoadMore()
  03-22 15:22:52.798 5957-5957/codsiga.com.xx D/xx: getDataFromServer()
  </code></pre>
  
  <p>What is wrong in the above code? Let me know if you need anything else. The same code worked well before.</p>