Created
May 2, 2019 10:50
-
-
Save kyryloz/ee40725d70af460b8b107457cbfd958e to your computer and use it in GitHub Desktop.
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 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