Skip to content

Instantly share code, notes, and snippets.

@indraAsLesmana
Last active November 20, 2020 02:49
Show Gist options
  • Save indraAsLesmana/d0d2b262cce6ee7d8b8ec5432cc7e7b8 to your computer and use it in GitHub Desktop.
Save indraAsLesmana/d0d2b262cce6ee7d8b8ec5432cc7e7b8 to your computer and use it in GitHub Desktop.
Create recyclerview margin, end, middle, start margin of item
import android.graphics.Rect
import androidx.recyclerview.widget.RecyclerView
import android.view.View
/**
* this widget add margin to start
* @param outReact.left to change margin start
*
* */
class RecyclerMargin(
private val start : Int = 0,
private val middle: Int = 16,
private val end: Int = 0
): RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
when {
parent.getChildAdapterPosition(view) == 0 -> {
outRect.left = start
outRect.right = middle
}
parent.getChildAdapterPosition(view) == parent.adapter?.itemCount?.minus(1) -> {
outRect.left = middle
outRect.right = end
}
else -> {
outRect.left = middle
outRect.right = middle
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment