title: Android 5.0 - Add header/footer to a RecyclerView
body: <p>I spent a moment trying to figure out a way to add a header to a <code>RecyclerView</code>, unsuccessfully.</p>
  
  <p>This is what I got so far:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      ...
  
      layouManager = new LinearLayoutManager(getActivity());
      recyclerView.setLayoutManager(layouManager);
  
      LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      headerPlaceHolder = inflater.inflate(R.layout.view_header_holder_medium, null, false);
      layouManager.addView(headerPlaceHolder, 0);
  
     ...
  }
  </code></pre>
  
  <p>The <code>LayoutManager</code> seems to be the object handling the disposition of the <code>RecyclerView</code> items. As I couldn't find any <code>addHeaderView(View view)</code> method, I decided to go with the <code>LayoutManager</code>'s <code>addView(View view, int position)</code> method and to add my header view in the first position to act as a header. </p>
  
  <p>Aaand this is where things get uglier:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.RecyclerView$ViewHolder android.support.v7.widget.RecyclerView$LayoutParams.mViewHolder' on a null object reference
      at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:2497)
      at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:4807)
      at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4803)
      at com.mathieumaree.showz.fragments.CategoryFragment.setRecyclerView(CategoryFragment.java:231)
      at com.mathieumaree.showz.fragments.CategoryFragment.access$200(CategoryFragment.java:47)
      at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:201)
      at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196)
      at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  </code></pre>
  
  <p>After getting several <code>NullPointerExceptions</code> trying to call the <code>addView(View view)</code> at different moments of the Activity creation (also tried adding the view once everything is set up, even the Adapter's data), I realized I have no idea if this is the right way to do it (and it doesn't look to be).</p>
  
  <p>PS: Also, a solution that could handle the <code>GridLayoutManager</code> in addition to the <code>LinearLayoutManager</code> would be really appreciated!</p>