Adding padding to a scrolling view, like RecyclerView, is a little bit trickier operation than with other Android views.
For example, if you add top and bottom padding to a RecyclerView like this:
<androidx.recyclerview.widget.RecyclerView ... android:paddingTop="16dp" android:paddingBottom="16dp" ... />
Then it’s going to behave as with any other view. I.e. the padding is added to the top and bottom of the view and it’s always visible:
But what if you want to add top padding to the first element and bottom padding to the last item?
That’s actually very easy to achieve! You just need to set the clipToPadding attribute to false in the RecyclerView:
<androidx.recyclerview.widget.RecyclerView ... android:clipToPadding="false" android:paddingTop="16dp" android:paddingBottom="16dp" ... />
With this change the padding is added to the first and last items:
When compared to the clipToPadding=“true”, the padding looks much nicer and it’s probably the behaviour you’re looking for.
This clipToPadding attribute also works if you are scrolling your data horizontally.