Skip to content

Instantly share code, notes, and snippets.

@kyryloz
Created May 2, 2019 10:50
Show Gist options
  • Save kyryloz/ee40725d70af460b8b107457cbfd958e to your computer and use it in GitHub Desktop.
Save kyryloz/ee40725d70af460b8b107457cbfd958e to your computer and use it in GitHub Desktop.
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class SpacingItemDecoration(
private val space: Int,
private val orientation: Int
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
if (orientation == RecyclerView.VERTICAL) {
outRect.bottom = space
// Add top margin only for the first item to avoid double space between items
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = space
}
} else {
outRect.right = space
// Add top margin only for the first item to avoid double space between items
if (parent.getChildAdapterPosition(view) == 0) {
outRect.left = space
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment