Last active
February 7, 2022 12:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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