Hey I want to show divider in top, middle and bottom in Recyclerview. How to add dividers and spaces between items in RecyclerView. It work to add divider in middle and bottom. But I cannot find to add divider on top of first item. Has anyone know how to achieve this? Thanks in advance.
import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.letsgetchecked.app.R
class SimpleDividerItemDecoration(private val context: Context) : RecyclerView.ItemDecoration() {
private var drawable: Drawable? = ContextCompat.getDrawable(context, R.drawable.cloudy)
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val left: Int = parent.paddingLeft
val right: Int = parent.width - parent.paddingRight
val childCount: Int = parent.childCount
for (i in 0 until childCount) {
val child: View = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val top: Int = child.bottom + params.bottomMargin
val bottom: Int = top + drawable?.intrinsicHeight!!
drawable?.setBounds(left, top, right, bottom)
drawable?.draw(c)
}
}
}
Expected Output
What I am getting