This is my custom Item Decoration class: DividerItemDecorator.kt
class DividerItemDecorator(private val mDivider: Drawable) : ItemDecoration() {
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 - 2) {
val child: View = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val dividerTop: Int = child.bottom + params.bottomMargin
val dividerBottom = dividerTop + mDivider.intrinsicHeight
mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
mDivider.draw(canvas)
}
}
}
This is how I add my custom Item Decoration to my recyclerView:
subNodeBody.addItemDecoration(DividerItemDecorator(ContextCompat.getDrawable(itemView.context,
R.drawable.recyclerview_divider)!!))
subNodeBody.adapter = RolesAdapter(subNode.roles)
This is the divider drawable: recyclerview_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#e7eef3"/>
<size android:height="0.7dp"/>
<size android:width="80dp"/>
</shape>
The divider looks proper between the RecyclerView items. But there's no space above and below it. Is there a way to add custom margins(top, bottom, right and left) around custom dividers?