Skip to content

Instantly share code, notes, and snippets.

@JoaquimLey
Last active June 12, 2019 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoaquimLey/9aca59bc1f2767a03dd0a82f58e967d8 to your computer and use it in GitHub Desktop.
Save JoaquimLey/9aca59bc1f2767a03dd0a82f58e967d8 to your computer and use it in GitHub Desktop.
open class RecyclerViewMarginItemDecoration(
private val sizeInDp: Int = 16,
private val isTop: Boolean = false
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (isTop) {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = sizeInDp.toPx()
}
} else {
if (parent.getChildAdapterPosition(view) == state.itemCount - 1) {
outRect.bottom = sizeInDp.toPx()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment