Skip to content

Instantly share code, notes, and snippets.

@twocity
Last active February 7, 2022 12:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twocity/b8b2cfcb0e85f8e2b06f to your computer and use it in GitHub Desktop.
Save twocity/b8b2cfcb0e85f8e2b06f to your computer and use it in GitHub Desktop.
An empty header (or footer) decoration for RecyclerView, since RecyclerView can't clipToPadding
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* An empty header (or footer) decoration for RecyclerView, since RecyclerView can't clipToPadding
*/
public class SimpleHeaderDecoration extends RecyclerView.ItemDecoration {
private final int headerHeight;
private final int footerHeight;
public SimpleHeaderDecoration(int headerHeight, int footerHeight) {
this.headerHeight = headerHeight;
this.footerHeight = footerHeight;
}
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
int childAdapterPosition = parent.getChildAdapterPosition(view);
if (childAdapterPosition == 0) {
outRect.top = headerHeight;
} else if (childAdapterPosition == parent.getAdapter().getItemCount() - 1) {
outRect.bottom = footerHeight;
} else {
outRect.set(0, 0, 0, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment