title: How to implement subheaders/sections inside a RecyclerView?
body: <p>I am trying to implement <a href="https://material.io/guidelines/components/subheaders.html#" rel="noreferrer">subheaders</a> inside my <code>RecyclerView</code>. As the adapter for my <code>RecyclerView</code>, I am using the <a href="https://developer.android.com/reference/android/arch/paging/PagedListAdapter.html" rel="noreferrer"><code>PagedListAdapter</code></a> from the brand new <a href="https://developer.android.com/topic/libraries/architecture/paging.html" rel="noreferrer">Paging Library</a> with the combination of <a href="https://developer.android.com/topic/libraries/architecture/room.html" rel="noreferrer">Room</a>.</p>
  
  <p>The data coming from the database contains dates. I want to separate/group the items by day and show the date inside the subheader.</p>
  
  <p>What would be the best way to implement <a href="https://material.io/guidelines/components/subheaders.html#" rel="noreferrer">subheaders</a> with this combination? AFAIK it is impossible to implement this the 'standard' ViewType way, because that would replace the first data list item with the header. At this moment I am trying to accomplish it with <code>RecyclerView.ItemDecoration</code>, but I am not able to make it work.</p>
  
  <p><strong>Can anyone point me in the right direction?</strong></p>
  
  <p>So far my <code>RecyclerView.ItemDecoration</code>:</p>
  
  <pre><code>public class RecyclerViewHeaderItemDecoration extends RecyclerView.ItemDecoration {
  
  private RecyclerViewHeaderReceiver receiver;
  
  public RecyclerViewHeaderItemDecoration setReceiver(RecyclerViewHeaderReceiver receiver) {
      this.receiver = receiver;
      return this;
  
  }
  
  @Override
  public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
  
      final int childCount = parent.getChildCount();
      for (int i = 0; i &lt; childCount; i++) {
          if (receiver != null) {
              View view = parent.getChildAt(i);
              int position = parent.getChildAdapterPosition(view);
              int previousPosition = position - 1;
              if (position == 0 || (position != RecyclerView.NO_POSITION &amp;&amp; receiver.isNewSection(previousPosition, position))) {
                  ViewHolder headerView = receiver.getSectionHeaderViewHolder(position);
                  parent.getAdapter().bindViewHolder(headerView, position); // I was not able to 'insert' a new viewHolder
              }
          }
      }
  }
  
  public interface RecyclerViewHeaderReceiver {
      boolean isNewSection(int prevPosition, int position);
      ViewHolder getSectionHeaderViewHolder(int position);
  }
   }
  </code></pre>