Skip to content

Instantly share code, notes, and snippets.

@yoonseopshin
Created February 13, 2022 10:10
Show Gist options
  • Save yoonseopshin/cae7f95207fff03d061ae460ba556e0e to your computer and use it in GitHub Desktop.
Save yoonseopshin/cae7f95207fff03d061ae460ba556e0e to your computer and use it in GitHub Desktop.
Add RecyclerView divider without last item
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.recyclerview.widget.RecyclerView
class ItemDecorationWithoutLastItem(private val divider: Drawable) : RecyclerView.ItemDecoration() {
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDrawOver(c, parent, state)
val left = parent.paddingLeft
val right = parent.width - parent.paddingRight
for (i in 0 until parent.childCount - 1) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val top = child.bottom + params.bottomMargin
val bottom = top + divider.intrinsicHeight
divider.setBounds(left, top, right, bottom)
divider.draw(c)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment