How to add a simple 8dp header/footer to Android's RecyclerView?

AndroidMarginPaddingAndroid Recyclerview

Android Problem Overview


Is there a way to add a simple header/footer to a RecyclerView?

Here you can see what I've got. The first Card touches the Toolbar This is what I've got

And here you can see what I want. 8dp Padding between the bottom and the Card. This is what I want to have

Methods I tried so far:

  • Use a header view in my recyclerview. But I think it is very unefficient to do this for every recyclerview.

  • use a 8dp top margin which results in the problem that the recyclerview has white bars on top/bottom when scrolling.

  • add a padding to the list item which results in different margins between the outer and inner cards.

  • I'm sure that there is a simple solution which I don't know so far.

    Android Solutions


    Solution 1 - Android

    Adding a top padding and setting clipToPadding to false will do the trick.
    Something like this:

      <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:paddingTop="8dp"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    Solution 2 - Android

    If you are using a RecyclerView with a layout_weight, and paddingBottom is not working for you, making sure you set the layout_height to 0dp! Otherwise, strangely, paddingTop works but paddingBottom does not:

    <android.support.v7.widget.RecyclerView android:id="@+id/recycler"
    	android:paddingBottom="20dp"
    	android:clipToPadding="false"
    	android:layout_weight="1"
    	android:layout_width="match_parent"
    	android:layout_height="0dp" /> 
    

    Attributions

    All content for this solution is sourced from the original question on Stackoverflow.

    The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

    Content TypeOriginal AuthorOriginal Content on Stackoverflow
    QuestionPhilipp SchumannView Question on Stackoverflow
    Solution 1 - AndroidflorianmskiView Answer on Stackoverflow
    Solution 2 - AndroidGreg EnnisView Answer on Stackoverflow