Last active
November 20, 2020 02:49
-
-
Save indraAsLesmana/d0d2b262cce6ee7d8b8ec5432cc7e7b8 to your computer and use it in GitHub Desktop.
Create recyclerview margin, end, middle, start margin of item
This file contains hidden or 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 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