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



    
    

你可能感兴趣的:(RecyclerView设置分割线---GridLayoutManager DividerItemDecoration)