Skip to content

Instantly share code, notes, and snippets.

@zakrodionov
Last active September 11, 2019 15:03
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 zakrodionov/ec5e483b76f055506421509dca166ed1 to your computer and use it in GitHub Desktop.
Save zakrodionov/ec5e483b76f055506421509dca166ed1 to your computer and use it in GitHub Desktop.
ItemDecoration #view
class HorizontalPaddingDecoration(
private val padding: Float = 2f,
private val firstLastPadding: Float = 20f
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val itemPosition = parent.getChildAdapterPosition(view)
if (itemPosition == RecyclerView.NO_POSITION) {
return
}
//outRect.top = padding.dpToPx()
//outRect.bottom = padding.dpToPx()
when {
itemPosition == 0 -> {
outRect.left = firstLastPadding.dpToPx()
outRect.right = padding.dpToPx()
}
itemPosition < state.itemCount - 1 -> {
outRect.left = padding.dpToPx()
outRect.right = padding.dpToPx()
}
itemPosition == state.itemCount - 1 -> {
outRect.left = padding.dpToPx()
outRect.right = firstLastPadding.dpToPx()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment