Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 25.08.17.
*/
class SimpleDividerDecorator(
private val dividerSizePx: Int,
dividerColor: Int
) : RecyclerView.ItemDecoration() {
private val paint = Paint().apply { color = dividerColor }
constructor(context: Context) : this(
context.resources.getDimensionPixelSize(R.dimen.divider_height),
ContextCompat.getColor(context, R.color.divider_color)
)
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) == 0) return
outRect.top = dividerSizePx
}
override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State?) {
val dividerLeft = parent.paddingLeft
val dividerRight = parent.width - parent.paddingRight
val childCount = parent.childCount
for (i in 0..childCount - 1) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val dividerTop = child.bottom + params.bottomMargin
val dividerBottom = dividerTop + dividerSizePx
val divRect = Rect(dividerLeft, dividerTop, dividerRight, dividerBottom)
canvas.drawRect(divRect, paint)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment