title: RecyclerView - Scroll To Position Not Working Every Time
body: <p>I have implemented a horizontal scrollable <code>RecyclerView</code>. My <code>RecyclerView</code> uses a <code>LinearLayoutManager</code>, and the problem I am facing is that when I try to use <code>scrollToPosition(position)</code> or <code>smoothScrollToPosition(position)</code> or from <code>LinearLayoutManager</code>'s <code>scrollToPositionWithOffset(position)</code>. Neither works for me. Either a scroll call doesn't scroll to the desired location or it doesn't invoke the <code>OnScrollListener</code>.</p>
  
  <p>So far I have tried so many different combinations of code that I cannot post them all here. Following is the one that works for me (But only partially):</p>
  
  <pre><code>public void smoothUserScrollTo(final int position) {
  
      if (position &lt; 0 || position &gt; getAdapter().getItemCount()) {
          Log.e(TAG, "An attempt to scroll out of adapter size has been stopped.");
          return;
      }
  
      if (getLayoutManager() == null) {
          Log.e(TAG, "Cannot scroll to position a LayoutManager is not set. " +
                  "Call setLayoutManager with a non-null layout.");
          return;
      }
  
      if (getChildAdapterPosition(getCenterView()) == position) {
          return;
      }
  
      stopScroll();
  
      scrollToPosition(position);
  
      if (lastScrollPosition == position) {
  
          addOnLayoutChangeListener(new OnLayoutChangeListener() {
              @Override
              public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
  
                  if (left == oldLeft &amp;&amp; right == oldRight &amp;&amp; top == oldTop &amp;&amp; bottom == oldBottom) {
                      removeOnLayoutChangeListener(this);
  
                      updateViews();
  
                      // removing the following line causes a position - 3 effect.
                      scrollToView(getChildAt(0));
                  }
              }
          });
      }
  
      lastScrollPosition = position;
  }
  
  @Override
  public void scrollToPosition(int position) {
      if (position &lt; 0 || position &gt; getAdapter().getItemCount()) {
          Log.e(TAG, "An attempt to scroll out of adapter size has been stopped.");
          return;
      }
  
      if (getLayoutManager() == null) {
          Log.e(TAG, "Cannot scroll to position a LayoutManager is not set. " +
                  "Call setLayoutManager with a non-null layout.");
          return;
      }
  
  //      stopScroll();
  
          ((LinearLayoutManager) getLayoutManager()).scrollToPositionWithOffset(position, 0);
  //        getLayoutManager().scrollToPosition(position);
      }
  </code></pre>
  
  <p>I opted for <code>scrollToPositionWithOffset()</code> because of <a href="https://stackoverflow.com/questions/30845742/smoothscrolltoposition-doesnt-work-properly-with-recyclerview">this</a> but the case perhaps is different as I use a LinearLayoutManager instead of GridLayoutManager. But the solution does work for me too, but as I said earlier only partially.</p>
  
  <ul>
  <li>When the call to scroll is from 0th position to totalSize - 7 scroll works like a charm.</li>
  <li>When scroll is from totalSize - 7 to totalSize - 3, First time I only scroll to 7th last item in the list. The second time however I can scroll fine</li>
  <li>When scrolling from totalSize - 3 to totalSize, I start getting unexpected behavior. </li>
  </ul>
  
  <p>If anyone has found a work around I'd Appreciate it. Here's the <a href="https://gist.github.com/abbasshah17/6239ae4b71112e82c5f3" rel="noreferrer">gist</a> to my code of custom <code>ReyclerView</code>.</p>