RecyclerView 添加下划线 Kotlin 教程 极速版

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView 添加下划线 Kotlin 教程 极速版相关的知识,希望对你有一定的参考价值。

下划线

public class DividerItemDecoration extends RecyclerView.ItemDecoration 
    private Drawable mDivider;

    public DividerItemDecoration(Context context) 
        mDivider = ContextCompat.getDrawable(context, R.drawable.divider);
    

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) 
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        for (int i = 0; i < parent.getChildCount() - 1; i++) 
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        
    

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:height="1dp" />
    <solid android:color="@color/color_grey" />
</shape>
<color name="color_grey">#f5f5f5</color>
        val dividerItemDecoration = DividerItemDecoration(this)
rv.addItemDecoration(dividerItemDecoration)

以上是关于RecyclerView 添加下划线 Kotlin 教程 极速版的主要内容,如果未能解决你的问题,请参考以下文章

Kotlin中实现RecyclerView嵌套RecyclerView

Kotlin 中 RecyclerView 中项目绑定的逻辑

尝试用kotlin做一个app

RecyclerView 动画问题 Android Kotlin

Android - 如何为 RecyclerView 添加点划线分隔线?

Kotlin recyclerview 分页重启(Pagination)