Created
September 6, 2019 11:47
-
-
Save ussernamenikita/fbdb0bb060d30fd6c0c1f777d3f8a978 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
package some.package | |
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.graphics.Paint | |
import android.graphics.Rect | |
import android.support.v7.widget.RecyclerView | |
import android.view.View | |
class ItemDecorator : RecyclerView.ItemDecoration() { | |
private val paint = Paint(Color.WHITE) | |
private val rectHeight = 20 | |
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { | |
if (needDrawBottomDelimiter(parent, view)) { | |
outRect.bottom = rectHeight | |
} else { | |
super.getItemOffsets(outRect, view, parent, state) | |
} | |
} | |
private fun needDrawBottomDelimiter(parent: RecyclerView, view: View): Boolean { | |
val viewPosition = parent.getChildAdapterPosition(view) | |
val lastItem = if (parent.adapter != null) parent.adapter!!.itemCount - 1 else 0 | |
return viewPosition == 0 || viewPosition == lastItem | |
} | |
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { | |
val dividerStart = parent.paddingStart | |
val dividerEnd = parent.width - parent.paddingEnd | |
val endIndex = parent.adapter?.itemCount ?: 0 | |
for (index in 0 until endIndex) { | |
val currentChild = parent.getChildAt(index) | |
if (needDrawBottomDelimiter(parent, currentChild)) { | |
val params = currentChild.layoutParams as RecyclerView.LayoutParams | |
val bottomView = currentChild.bottom + params.bottomMargin | |
val startX = dividerStart.toFloat() | |
val startY = bottomView.toFloat() | |
val endX = dividerEnd.toFloat() | |
val endY = startY + rectHeight | |
c.drawRect(startX, startY, endX, endY, paint) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment