带有ItemDecoration的RecyclerView分隔线宽度

时间:2018-09-21 04:38:08

标签: android-recyclerview width recyclerview-layout divider item-decoration

我有一个RecyclerView,并添加了一个ItemDecoration如下:-

mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

我的ItemDecoration看起来像:-

public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
    {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
}

这将绘制一个占据整个屏幕宽度的分隔线。我想从左右增加一点空白,大约15dp(红线),以使其看起来比屏幕宽度短一些,如下所示:-

enter image description here

我该如何实现?任何建议表示赞赏。

2 个答案:

答案 0 :(得分:0)

看看getItemOffsets方法。像下面这样的事情应该做。

override fun getItemOffsets(
    rect: Rect,
    view: View,
    parent: RecyclerView,
    state: RecyclerView.State
) {
    rect.right = parent.right - yourMargin
    rect.left = parent.left + yourMargin
}

此方法将设置您要在其上绘制(onDraw(...))装饰的矩形空间。

答案 1 :(得分:0)

只需输入

的值
    final int left = 200;
    final int right= 200;