在 Recyclerview Kotlin 的顶部、中间和底部添加分隔线

Add Divider Top,Middle and Bottom of Recyclerview Kotlin

嘿,我想在 Recyclerview 的顶部、中间和底部显示分隔线。 How to add dividers and spaces between items in RecyclerView。它可以在中间和底部添加分隔线。但我找不到在第一项之上添加分隔符。有谁知道如何实现这一目标?提前致谢。

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)
        }
    }
}

预期输出

我得到了什么

您不需要自己编写 class。你这样做的方式并没有考虑分隔线的厚度,所以它可能会导致你的列表项中的填充不准确。使用提供的 class DividerItemDecoration。

但是,它还缺少顶部项目上方的分隔线。这里有一个 class,您可以将其添加为第二个装饰。它仅在第一个项目上绘制分隔线,因此您可以自定义它以使其看起来与其他项目不同,出于可用性原因,我认为这是一个好主意。我将其基于 DividerItemDecoration 的来源,因此它正确地说明了填充和裁剪。

class TopDividerItemDecoration(val context: Context) : RecyclerView.ItemDecoration() {
    private val bounds = Rect()
    private var _drawable: Drawable? =
        context.obtainStyledAttributes(intArrayOf(android.R.attr.listDivider)).use {
            it.getDrawable(0)
        }
    var drawable: Drawable
        get() = _drawable
            ?: error("A drawable must be set before use. Current theme lacks default divider.")
        set(value) {
            _drawable = value
        }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.layoutManager == null || parent.childCount == 0)
            return

        c.save()
        if (parent.clipToPadding) {
            c.clipRect(
                parent.paddingLeft, parent.paddingTop, parent.width - parent.paddingRight,
                parent.height - parent.paddingBottom
            )
        }

        val child = parent.getChildAt(0)
        parent.getDecoratedBoundsWithMargins(child, bounds)
        val top = bounds.top + child.translationY.roundToInt()
        val bottom = top + drawable.intrinsicHeight
        drawable.setBounds(0, top, parent.width, bottom)
        drawable.draw(c)

        c.restore()
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.getChildLayoutPosition(view) == 0) {
            outRect.top += drawable.intrinsicHeight
        }
    }
}