Ответ
Для добавления разделителей в RecyclerView можно использовать ItemDecoration. Пример реализации:
class DividerItemDecoration(
private val dividerHeight: Int,
private val color: Int
) : RecyclerView.ItemDecoration() {
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.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 + dividerHeight
c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), Paint().apply {
this.color = color
})
}
}
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
outRect.bottom = dividerHeight
}
}
Использование:
recyclerView.addItemDecoration(
DividerItemDecoration(
dividerHeight = 2.dpToPx(context),
color = Color.GRAY
)
)
Альтернативно можно использовать готовый DividerItemDecoration из support library:
recyclerView.addItemDecoration(
DividerItemDecoration(context, LinearLayoutManager.VERTICAL)
)