Android:将divider添加到RecyclerView的顶部

时间:2018-02-14 00:54:39

标签: android android-recyclerview kotlin

我能够在RecyclerView

的细胞底部添加分隔线
this.recyclerView.layoutManager = layoutManager
val dividerItemDecoration = DividerItemDecoration(this.recyclerView.context, layoutManager.orientation)
this.recyclerView.addItemDecoration(dividerItemDecoration)

结果如下:

enter image description here

如何在第一个单元格的顶部添加一行?

1 个答案:

答案 0 :(得分:0)

首先,您不应在页眉和项目/页脚和项目之间创建相同的分隔符。如果可以选择,用户会感到困惑。为了获得良好的用户界面,我建议在页眉/页脚上制作彩色背景。像这样:

enter image description here

如果您坚持在标题中添加分隔符,则可以在ItemDecoration上编辑它,就像项目分隔符一样。将此分隔符用于标题项目装饰。更多详细信息,请访问DividerItemDecoration

fun drawVertical(Canvas c, RecyclerView parent) {
        val left = parent.getPaddingLeft()
        val right = parent.getWidth() - parent.getPaddingRight()

        val childCount = parent.getChildCount()
        val child : View = parent.getChildAt(i)
        val params : RecyclerView.LayoutParams = child.getLayoutParams();
        val top = child.getBottom() + params.bottomMargin;
        val bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }