Created
July 12, 2017 04:43
-
-
Save baleen37/00307967b6eb3d20bc5343354b61fdaa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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