Skip to content

Instantly share code, notes, and snippets.

@rock3r
Created September 4, 2018 11:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rock3r/ab153e17ec4cc213714084bf32f83e5e to your computer and use it in GitHub Desktop.
Save rock3r/ab153e17ec4cc213714084bf32f83e5e to your computer and use it in GitHub Desktop.
A simple yet fully featured RecyclerView ItemDecorator that draws a divider line between items. Only works with vertical LinearLayoutManagers!
package me.seebrock3r.common.widget
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.Px
import androidx.core.graphics.withTranslation
import androidx.core.view.children
import androidx.recyclerview.widget.RecyclerView
class DividerItemDecorator(
@param:ColorInt @field:ColorInt private val strokeColor: Int,
@param:Px @field:Px private val strokeWidth: Int,
@param:Px @field:Px private val marginStart: Float = 0.0F,
@param:Px @field:Px private val marginEnd: Float = marginStart,
@param:Px @field:Px private val horizontalSpacing: Int,
@param:Px @field:Px private val verticalSpacing: Int = horizontalSpacing
) : RecyclerView.ItemDecoration() {
private val dividerPaint = Paint().apply {
color = strokeColor
flags = Paint.ANTI_ALIAS_FLAG
}
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
outRect.set(horizontalSpacing, verticalSpacing, horizontalSpacing, verticalSpacing)
}
override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
parent.children.forEach { child ->
if (child.isNotLastItem(parent, state.itemCount))
canvas.withTranslation(x = 0.0F, y = verticalSpacing.toFloat()) {
val lineY = child.bottom - strokeWidth / 2.0F
drawLine(
child.left + marginStart,
lineY,
child.right - marginEnd,
lineY,
dividerPaint
)
}
}
}
private fun View.isNotLastItem(parent: RecyclerView, itemCount: Int) =
parent.getChildAdapterPosition(this) < itemCount - 1
}
@juanchosaravia
Copy link

This is great! thanks for sharing!
I would slightly change some small details:

  • move state.itemCount out the loop to avoid calling this method all the time.
  • substract 1 to itemCount to ignore last item
  • do a forEachIndexed to avoid calling parent.getChildAdapterPosition(this)
    override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val itemCount = state.itemCount - 1 // this is to ignore last item
        parent.forEachIndexed { index, child ->
            if (index < itemCount) {
                canvas.withTranslation(x = 0.0F, y = verticalSpacing.toFloat()) {
                    val lineY = child.bottom - strokeWidth / 2.0F
                    drawLine(
                            child.left + marginStart,
                            lineY,
                            child.right - marginEnd,
                            lineY,
                            dividerPaint
                    )
                }
            }
        }
    }

Please let me know your thoughts and thanks again!

@deniszink
Copy link

This is great! thanks for sharing!
I would slightly change some small details:

  • move state.itemCount out the loop to avoid calling this method all the time.
  • substract 1 to itemCount to ignore last item
  • do a forEachIndexed to avoid calling parent.getChildAdapterPosition(this)
    override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val itemCount = state.itemCount - 1 // this is to ignore last item
        parent.forEachIndexed { index, child ->
            if (index < itemCount) {
                canvas.withTranslation(x = 0.0F, y = verticalSpacing.toFloat()) {
                    val lineY = child.bottom - strokeWidth / 2.0F
                    drawLine(
                            child.left + marginStart,
                            lineY,
                            child.right - marginEnd,
                            lineY,
                            dividerPaint
                    )
                }
            }
        }
    }

Please let me know your thoughts and thanks again!

How did you call forEachIndexed from the RecyclerView ? Cause this is not a collection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment