美文网首页
RecyclerView设置分割线---GridLayoutMa

RecyclerView设置分割线---GridLayoutMa

作者: s酸菜 | 来源:发表于2020-04-27 16:45 被阅读0次

    给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

    相关文章

      网友评论

          本文标题:RecyclerView设置分割线---GridLayoutMa

          本文链接:https://www.haomeiwen.com/subject/rjhuwhtx.html