title: RecyclerView decorator adding extra padding on refresh
body: <p>So, I'm facing a weird problem while implementing RecyclerView in my project. I have a custom decorator to implement a consistent top and bottom padding and a rather different value of padding in between elements. Whenever I tap on refresh, i.e. fetch data from back-end and populate RecyclerView again, the padding increases and it keeps on increasing on each refresh.</p>
  
  <p><img src="https://i.stack.imgur.com/cGKeC.png" alt="This is the correct way"></p>
  
  <p>When I hit refresh (causing AsyncTask to execute again), the space between items increase. And it keeps on increase with each refresh.</p>
  
  <p><img src="https://i.stack.imgur.com/diePY.png" alt="Padding between items increasing"></p>
  
  <p>I have a typical RecyclerView like this</p>
  
  <pre><code>&lt;android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="false"
      android:focusableInTouchMode="false"&gt;
  &lt;/android.support.v7.widget.RecyclerView&gt;
  </code></pre>
  
  <p>Populating content inside RecyclerView is CardView with the following layout:</p>
  
  <pre><code>&lt;android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/cv"
  android:layout_marginLeft="9dp"
  android:layout_marginRight="9dp"
  &gt;
  
      &lt;RelativeLayout
          android:id="@+id/itemLinearTop"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:paddingTop="17dp"
          android:paddingRight="10dp"
          android:paddingLeft="10dp"&gt;
  
          &lt;TextView
              android:id="@+id/tv1"
              android:textColor="@color/logo_black"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  
          &lt;TextView
              android:id="@+id/tv2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/tv1"
              android:textColor="@color/logo_black"
              android:layout_alignParentRight="true"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;!-- More elements --&gt;
      &lt;/RelativeLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  </code></pre>
  
  <p>I am initializing and populating RecyclerView like this:</p>
  
  <pre><code>// In PostExecute of AsyncTask
  RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
  RecycleViewAdapter adapter = new FuelPricesRecycleViewAdapter(data);
  rv.setAdapter(adapter);
  rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1));
  </code></pre>
  
  <p>And this is what my RecyclerViewItemDecoration class looks like:</p>
  
  <pre><code>public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {
      private int space = 0;
      private int item_count = 0;
      private int ADDITIONAL_PADDING = 20;
  
      public RecyclerViewItemDecoration(int space, int item_count) {
          this.space = space;
          this.item_count = item_count;
      }
  
      @Override
      public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
          if(parent.getChildPosition(view) != item_count)
               outRect.bottom = space;
  
           // Add top margin only for the first item to avoid double space between items
           if(parent.getChildPosition(view) == 0)
               outRect.top = space + ADDITIONAL_PADDING;
           if(parent.getChildPosition(view) == item_count)
               outRect.bottom = space + ADDITIONAL_PADDING;
       }
   }
  </code></pre>
  
  <p>Thank you for your help!</p>