Created
March 28, 2022 05:28
-
-
Save KryptKode/dc4a7f353d7d328707cd7378847f3fb4 to your computer and use it in GitHub Desktop.
Item decorator for spaces in a RecyclerView
This file contains 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
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