Skip to content

Instantly share code, notes, and snippets.

@cmorigaki
Last active May 25, 2020 12:42
Show Gist options
  • Select an option

  • Save cmorigaki/2a2eacd2724e69e08778d1be0f5c1cbc to your computer and use it in GitHub Desktop.

Select an option

Save cmorigaki/2a2eacd2724e69e08778d1be0f5c1cbc to your computer and use it in GitHub Desktop.
Margin Item Decoration (GridLayoutManager also)
class MarginItemDecoration(
private val spaceSize: Int,
private val spanCount: Int = 1,
private val orientation: Int = GridLayoutManager.VERTICAL
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State
) {
with(outRect) {
if (orientation == GridLayoutManager.VERTICAL) {
if (parent.getChildAdapterPosition(view) < spanCount) {
top = spaceSize
}
if (parent.getChildAdapterPosition(view) % spanCount == 0) {
left = spaceSize
}
} else {
if (parent.getChildAdapterPosition(view) < spanCount) {
left = spaceSize
}
if (parent.getChildAdapterPosition(view) % spanCount == 0) {
top = spaceSize
}
}
right = spaceSize
bottom = spaceSize
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment