添加循环视图粗线的顶部、中间和底部分割线 [英] Add Divider Top,Middle and Bottom of Recyclerview Kotlin

查看:0
本文介绍了添加循环视图粗线的顶部、中间和底部分割线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想在循环视图中显示顶部、中间和底部的分隔符。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)
        }
    }
}

预期产量

我得到的是什么

推荐答案

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

但是,它还缺少顶部项目上方的分隔符。这里有一个类,您可以将其添加为第二个装饰。它只在第一个项目上画了一个分隔线,这样你就可以定制它,让它看起来与其他项目不同,出于可用性的原因,我认为这是一个好主意。我将它建立在DividerItemDecation的源代码基础上,因此它正确地考虑了填充和剪辑。

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

这篇关于添加循环视图粗线的顶部、中间和底部分割线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆