小编Era*_*per的帖子

如何在 Kotlin 中为 RecyclerView 添加项目分隔符

我正在开发一个应用程序,其中有一个包含 recyclerview 的列表,我想为项目添加分隔符。我已经创建了 ItemDividerDecorator 类和 xml 布局文件,但我没有连接到回收器视图。

我知道在java中怎么做,像这样:

recyclerView.addItemDecoration(
                new DividerItemDecoration(ContextCompat.getDrawable(getApplicationContext(),
                        R.drawable.item_separator)));
Run Code Online (Sandbox Code Playgroud)

但是我怎么能在 Kotlin 中做,我也尝试在 Android Studio 中转换它,但显示了一些错误。这是我的装饰器类:

    private val mdivider:Drawable
    init{
        this.mdivider = mdivider
    }
    override fun onDrawOver(canvas: Canvas, parent:RecyclerView, state:RecyclerView.State) {
        val left = parent.getPaddingLeft()
        val right = parent.getWidth() - parent.getPaddingRight()
        val childCount = parent.getChildCount()
        for (i in 0 until childCount)
        {
            val child = parent.getChildAt(i)
            val params = child.getLayoutParams() as RecyclerView.LayoutParams
            val top = child.getBottom() + params.bottomMargin
            val bottom = top + mdivider.getIntrinsicHeight()
            mdivider.setBounds(left, top, right, bottom) …
Run Code Online (Sandbox Code Playgroud)

android kotlin

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

kotlin ×1