Skip to content

Instantly share code, notes, and snippets.

@KryptKode
Created March 28, 2022 05:28
Show Gist options
  • Save KryptKode/dc4a7f353d7d328707cd7378847f3fb4 to your computer and use it in GitHub Desktop.
Save KryptKode/dc4a7f353d7d328707cd7378847f3fb4 to your computer and use it in GitHub Desktop.
Item decorator for spaces in a RecyclerView
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class SpacesItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State) {
outRect.left = space
outRect.right = space
outRect.bottom = space
// Add top margin only for the first item to avoid double space between items
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = space
} else {
outRect.top = 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment