RecyclerView设置分割线---GridLayoutManager DividerItemDecoration

给GridLayoutManager 设置简单的分割线

mBinding.rvWorkbench.addItemDecoration(
            DividerItemDecoration(
                activity,
                RecyclerView.VERTICAL
            )
        )

这样设置只能是出现在垂直方向上,缺少了水平方向的

其实我们可以再次设置一次

mBinding.rvWorkbench.addItemDecoration(
            DividerItemDecoration(
                activity,
                RecyclerView.HORIZONTAL
            )
        )

这样 水平 垂直方向上都有了

当然 你也可以自定义shape


        val dividerVertical = DividerItemDecoration(activity, RecyclerView.VERTICAL)
        dividerVertical.setDrawable(resources.getDrawable(R.drawable.shape_item_decoration))
        mBinding.rvWorkbench.addItemDecoration(dividerVertical)

        val dividerHorizontal = DividerItemDecoration(activity, RecyclerView.HORIZONTAL)
        dividerHorizontal.setDrawable(resources.getDrawable(R.drawable.shape_item_decoration))
        mBinding.rvWorkbench.addItemDecoration(dividerHorizontal)

shape_item_decoration.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffd1d1d6" />
    <size
        android:width="1dp"
        android:height="1dp" />
</shape>
QQ20200427-164508@2x.png

推荐阅读更多精彩内容