Created
February 13, 2022 10:10
-
-
Save yoonseopshin/cae7f95207fff03d061ae460ba556e0e to your computer and use it in GitHub Desktop.
Add RecyclerView divider without last item
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.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