【问题标题】:Android: add divider to top of RecyclerViewAndroid:在 RecyclerView 顶部添加分隔线
【发布时间】:2018-02-14 00:54:39
【问题描述】:

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

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

结果如下:

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

【问题讨论】:

    标签: android android-recyclerview kotlin


    【解决方案1】:

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

    如果您坚持在标题中添加分隔符,您可以在 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      相关资源
      最近更新 更多