title: Android add spacing below last element in recyclerview with gridlayoutmanager
body: <p>I am trying to add spacing below the last element row in <code>RecyclerView</code> with <code>GridLayoutManager</code>. I used custom <code>ItemDecoration</code> for this purpose with bottom padding when its last element as follows:</p>
  
  <pre><code>public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
  private int space;
  private int bottomSpace = 0;
  
  public SpaceItemDecoration(int space, int bottomSpace) {
      this.space = space;
      this.bottomSpace = bottomSpace;
  }
  
  public SpaceItemDecoration(int space) {
      this.space = space;
      this.bottomSpace = 0;
  }
  
  @Override
  public void getItemOffsets(Rect outRect, View view,
                             RecyclerView parent, RecyclerView.State state) {
  
      int childCount = parent.getChildCount();
      final int itemPosition = parent.getChildAdapterPosition(view);
      final int itemCount = state.getItemCount();
  
      outRect.left = space;
      outRect.right = space;
      outRect.bottom = space;
      outRect.top = space;
  
      if (itemCount &gt; 0 &amp;&amp; itemPosition == itemCount - 1) {
          outRect.bottom = bottomSpace;
      }
  }
  }
  </code></pre>
  
  <p>But the problem with this method is that it messed up the element heights in the grid in last row. I am guessing that <code>GridLayoutManager</code> changes the heights for elements based on spacing left. What is the correct way to achieve this?</p>
  
  <p>This will work correctly for a <code>LinearLayoutManager</code>. Just in case of a <code>GridLayoutManager</code> its problematic.</p>
  
  <p>Its very useful in case you have a <code>FAB</code> in bottom and need items in last row to scroll above <code>FAB</code> so that they can be visible.</p>