Skip to content

Instantly share code, notes, and snippets.

@Miha-x64
Created July 13, 2017 11:43
Show Gist options
  • Save Miha-x64/4e8754e72a1e65e3ca742744ea501f16 to your computer and use it in GitHub Desktop.
Save Miha-x64/4e8754e72a1e65e3ca742744ea501f16 to your computer and use it in GitHub Desktop.
RecyclerView decoration which adds indentation in the beginning and in the end of content.
package net.aquadc.commonandroid.lists;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class FirstLastItemSpacesDecoration extends RecyclerView.ItemDecoration {
private final int directSpace;
private final int reverseSpace;
public FirstLastItemSpacesDecoration(int space, boolean layoutReversed) {
if (layoutReversed) {
directSpace = 0;
reverseSpace = space;
} else {
directSpace = space;
reverseSpace = 0;
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) == state.getItemCount()-1) {
outRect.bottom = directSpace;
outRect.top = reverseSpace;
}
if (parent.getChildAdapterPosition(view) == 0) {
outRect.bottom = reverseSpace;
outRect.top = directSpace;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment