Created
August 25, 2017 16:23
-
-
Save terrakok/4098e204923bc4f773c8504c4b424aec to your computer and use it in GitHub Desktop.
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
/** | |
* 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