Skip to content

Instantly share code, notes, and snippets.

@baleen37
Created July 12, 2017 04:43
Show Gist options
  • Save baleen37/00307967b6eb3d20bc5343354b61fdaa to your computer and use it in GitHub Desktop.
Save baleen37/00307967b6eb3d20bc5343354b61fdaa to your computer and use it in GitHub Desktop.
class SpacesItemDecoration(val outerOffset: Int,
val innerOffset: Int,
val orientationType: OrientationType = VERTICAL) : RecyclerView.ItemDecoration() {
enum class OrientationType {
VERTICAL, HORIZON
}
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView,
state: RecyclerView.State) {
if (orientationType == VERTICAL) {
if (parent.childCount == 1) {
outRect.top = outerOffset
outRect.bottom = innerOffset
} else if (parent.getChildAdapterPosition(view) == state.itemCount - 1) {
outRect.top = 0
outRect.bottom = outerOffset
} else if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = outerOffset
outRect.bottom = innerOffset
} else {
outRect.bottom = innerOffset
}
} else {
if (parent.childCount == 1) {
outRect.left = outerOffset
outRect.right = innerOffset
} else if (parent.getChildAdapterPosition(view) == state.itemCount - 1) {
outRect.right = outerOffset
outRect.left = 0
} else if (parent.getChildAdapterPosition(view) == 0) {
outRect.left = outerOffset
outRect.right = innerOffset
} else {
outRect.right = innerOffset
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment