title: Recycler view inside NestedScrollView causes scroll to start in the middle
body: <p>I am getting a weird scrolling behavior when I add a RecyclerView inside a NestedScrollView.</p>
  
  <p>What happens is that whenever the scrollview has more rows than can be shown in the screen, as soon as the activity is launched, the NestedScrollView starts with an offset from the top (image 1). If there are few items in the scroll view so that they can all be shown at once, this doesn't happen (image 2).</p>
  
  <p>I am using version 23.2.0 of the support library.</p>
  
  <p><strong>Image 1</strong>: WRONG - starts with offset from the top</p>
  
  <p><img src="https://imageshack.com/i/pn12bxakp" alt="Image 1"></p>
  
  <p><strong>Image 2</strong>: CORRECT - few items in the recycler view</p>
  
  <p><img src="https://imageshack.com/i/podbldWDp" alt="Image 2"></p>
  
  <p>I am pasting below my layout code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="fill_vertical"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="10dp"&gt;
  
              &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:padding="16dp"
                  android:orientation="vertical"&gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="Title:"
                      style="@style/TextAppearance.AppCompat.Caption"/&gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:padding="@dimen/bodyPadding"
                      style="@style/TextAppearance.AppCompat.Body1"
                      android:text="Neque porro quisquam est qui dolorem ipsum"/&gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="Subtitle:"
                      style="@style/TextAppearance.AppCompat.Caption"/&gt;
  
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      style="@style/TextAppearance.AppCompat.Body1"
                      android:padding="@dimen/bodyPadding"
                      android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."/&gt;
  
              &lt;/LinearLayout&gt;
  
          &lt;android.support.v7.widget.RecyclerView
              android:id="@+id/rv"
              android:focusable="false"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/android.support.v4.widget.NestedScrollView&gt;
  </code></pre>
  
  <p>Am I missing something? Does anyone have any idea how to fix this?</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>It works correctly if I place the following code when initializing my Activity:</p>
  
  <pre><code>sv.post(new Runnable() {
          @Override
          public void run() {
              sv.scrollTo(0,0);
          }
  });
  </code></pre>
  
  <p>Where sv is a reference to the NestedScrollView, however it looks like quite a hack.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>As requested, here is my adapter code:</p>
  
  <pre><code>public abstract class ArrayAdapter&lt;T, VH extends RecyclerView.ViewHolder&gt;
          extends RecyclerView.Adapter&lt;VH&gt; {
  
      private List&lt;T&gt; mObjects;
  
      public ArrayAdapter(final List&lt;T&gt; objects) {
          mObjects = objects;
      }
  
      /**
       * Adds the specified object at the end of the array.
       *
       * @param object The object to add at the end of the array.
       */
      public void add(final T object) {
          mObjects.add(object);
          notifyItemInserted(getItemCount() - 1);
      }
  
      /**
       * Remove all elements from the list.
       */
      public void clear() {
          final int size = getItemCount();
          mObjects.clear();
          notifyItemRangeRemoved(0, size);
      }
  
      @Override
      public int getItemCount() {
          return mObjects.size();
      }
  
      public T getItem(final int position) {
          return mObjects.get(position);
      }
  
      public long getItemId(final int position) {
          return position;
      }
  
      /**
       * Returns the position of the specified item in the array.
       *
       * @param item The item to retrieve the position of.
       * @return The position of the specified item.
       */
      public int getPosition(final T item) {
          return mObjects.indexOf(item);
      }
  
      /**
       * Inserts the specified object at the specified index in the array.
       *
       * @param object The object to insert into the array.
       * @param index  The index at which the object must be inserted.
       */
      public void insert(final T object, int index) {
          mObjects.add(index, object);
          notifyItemInserted(index);
  
      }
  
      /**
       * Removes the specified object from the array.
       *
       * @param object The object to remove.
       */
      public void remove(T object) {
          final int position = getPosition(object);
          mObjects.remove(object);
          notifyItemRemoved(position);
      }
  
      /**
       * Sorts the content of this adapter using the specified comparator.
       *
       * @param comparator The comparator used to sort the objects contained in this adapter.
       */
      public void sort(Comparator&lt;? super T&gt; comparator) {
          Collections.sort(mObjects, comparator);
          notifyItemRangeChanged(0, getItemCount());
      }
  }
  </code></pre>
  
  <p>And here is my ViewHolder:</p>
  
  <pre><code>public class ViewHolder extends RecyclerView.ViewHolder {
      private TextView txt;
      public ViewHolder(View itemView) {
          super(itemView);
          txt = (TextView) itemView;
      }
  
      public void render(String text) {
          txt.setText(text);
      }
  }
  </code></pre>
  
  <p>And here is the layout of each item in the RecyclerView (it's just <code>android.R.layout.simple_spinner_item</code> - this screen is only for showing an example of this bug):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@android:id/text1"
      style="?android:attr/spinnerItemStyle"
      android:singleLine="true"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:ellipsize="marquee"
      android:textAlignment="inherit"/&gt;
  </code></pre>