title: Get child view height in RecyclerView ItemDecoration.getItemOffsets
body: <p>I am attempting to create an ItemDecoration that will maintain a minimum height for a RecyclerView by dynamically adding padding to the last item.</p>
  
  <p>To calculate the amount of padding I need to know the total height of all children. I am approximating this by getting the height of a single view and multiplying it by the number of items in the Adapter.</p>
  
  <p>The problem I am experiencing is that view.getHeight() doesn't always return the correct height. Usually the first time getItemOffsets() is called the height is much smaller than it should be (e.g. 30px vs 300px). This happens even if I give the child views a fixed height in the layout XML. I am guessing this has something to do with the measure/layout cycle by I am unsure of how to get the correct view dimensions in the ItemDecoration.</p>
  
  <p>What is the correct way to get the child views height programmatically in getItemOffsets()?</p>
  
  <p>ItemDecoration:</p>
  
  <pre><code>public class MinHeightPaddingItemDecoration extends RecyclerView.ItemDecoration {
      private static final String LOG_TAG = MinHeightPaddingItemDecoration.class.getSimpleName();
  
      private int mMinHeight;
  
      public MinHeightPaddingItemDecoration(int minHeight) {
          super();
          mMinHeight = minHeight;
      }
  
      @Override
      public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
          int itemCount = state.getItemCount();
          int lastPosition = itemCount - 1;
          int itemPosition = recyclerView.getChildAdapterPosition(view);
          int layoutPosition = recyclerView.getChildLayoutPosition(view);
  
          // If this view isnt on screen then do nothing
          if (layoutPosition != RecyclerView.NO_POSITION &amp;&amp; itemPosition == lastPosition) {
  
              // NOTE: view.getHeight() doesn't always return the correct height, even if the layout is given a fixed height in the XML
              int childHeight = view.getHeight();
  
              int totalChildHeight = childHeight * itemCount;
  
              int minHeight = getMinHeight();
              if (totalChildHeight &lt; minHeight) {
                  outRect.bottom = minHeight - totalChildHeight;
              }
          }
      }
  
      private int getMinHeight() {
          return mMinHeight;
      }
  }
  </code></pre>
  
  <p>recycler_view_item.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"&gt;
      &lt;data&gt;
          &lt;variable name="controller" type="my.ViewController"/&gt;
      &lt;/data&gt;
  
      &lt;android.support.v7.widget.CardView
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:layout_gravity="center"
          android:clickable="true"
          android:onClick="@{() -&gt; controller.doSomething()}"&gt;
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
                  &lt;TextView
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="Child layout"
                      /&gt;
          &lt;/RelativeLayout&gt;
  
      &lt;/android.support.v7.widget.CardView&gt;
  &lt;/layout&gt;
  </code></pre>