Android:在 RecyclerView 顶部添加分隔线 [英] Android: add divider to top of RecyclerView

查看:53
本文介绍了Android:在 RecyclerView 顶部添加分隔线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在 RecyclerView 中的单元格底部添加分隔线:

I was able to add dividers on the bottoms of cells in a RecyclerView:

this.recyclerView.layoutManager = layoutManager
val dividerItemDecoration = DividerItemDecoration(this.recyclerView.context, layoutManager.orientation)
this.recyclerView.addItemDecoration(dividerItemDecoration)

结果如下:

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

How can I add a line to the top of the very first cell?

推荐答案

首先,你不应该在 Header 和 items/Footer 和 items 之间做相同的分隔线.如果可以选择,用户会感到困惑.为了获得良好的用户界面,我建议在页眉/页脚上制作彩色背景.喜欢这个:

First, you shouldn't make same divider between Header and items/Footer and items. User will get confused if it could be selected or not. For a good User Interface, I recommended to make colour background on header/footer. Like this one :

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

If you insist on adding the divider in header, you could edit it on your ItemDecoration just like the item divider. Use this divider for your Header Item Decoration. More detail visit 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);
    }

这篇关于Android:在 RecyclerView 顶部添加分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆