Android - 如何为RecyclerView添加虚线/点线分隔线?

时间:2016-09-23 18:17:54

标签: android android-recyclerview

我使用this answer中的代码为RecyclerView创建了一个固定的分隔线。

但是,我希望这条线是虚线/点线。

我已经在我的应用中的其他位置使用了line_dashed.xml资源:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line" >

    <stroke
        android:color="@color/blue"
        android:dashGap="12dp"
        android:dashWidth="12dp"
        android:width="1dp" />

</shape>

但是,如果我尝试将此作为通过调用recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext()))访问的可绘制资源,则根本不会绘制任何行。

如何解决这样的虚线?

3 个答案:

答案 0 :(得分:5)

只需将您的drawable资源添加到此项目装饰器中。

DividerItemDecoration decorator = new DividerItemDecoration(ContextCompat.getDrawable(getContext(), R.drawable.line_dashed));
recyclerView.addItemDecoration(decorator);

和Div​​iderItemDecorator类:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int mPaddingLeft;

    public DividerItemDecoration(Drawable divider) {
        mDivider = divider;
        mPaddingLeft = 0;
    }

    public DividerItemDecoration(Drawable divider, int paddingLeft) {
        mDivider = divider;
        mPaddingLeft = paddingLeft;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (mDivider == null) return;
        if (parent.getChildAdapterPosition(view) < 1) return;

        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            outRect.top = mDivider.getIntrinsicHeight();
        } else {
            outRect.left = mDivider.getIntrinsicWidth();
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mDivider == null) {
            super.onDrawOver(c, parent, state);
            return;
        }

        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            final int left = parent.getPaddingLeft() + mPaddingLeft;
            final int right = parent.getWidth() - parent.getPaddingRight();
            final int childCount = parent.getChildCount();

            for (int i = 1; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int size = mDivider.getIntrinsicHeight();
                final int top = child.getTop() - params.topMargin;
                final int bottom = top + size;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }

        } else { //horizontal
            final int top = parent.getPaddingTop();
            final int bottom = parent.getHeight() - parent.getPaddingBottom();
            final int childCount = parent.getChildCount();

            for (int i = 1; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int size = mDivider.getIntrinsicWidth();
                final int left = child.getLeft() - params.leftMargin;
                final int right = left + size;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

    private int getOrientation(RecyclerView parent) {
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            return layoutManager.getOrientation();
        } else
            throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager.");
    }
}

我应该测试它。

<强>更新

    android:layerType="software"

在xml中为recyclerView添加此参数 还可以在您的形状可绘制中添加尺寸:

<size android:height="1dp"/>

答案 1 :(得分:2)

当前,您可以在框中使用DividerItemDecoration。

recyclerView.apply {
        layoutManager = LinearLayoutManager(this@YourFragment.context)
        adapter = this@YourFragment.adapter
        addItemDecoration(
            DividerItemDecoration(
                this@YourFragment.context,
                DividerItemDecoration.VERTICAL
            ).apply {
                context.getDrawable(R.drawable.divider)?.let {
                    setDrawable(it)
                }
            }
        )
    }

使用下一个形状XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">

    <size android:height="1dp" />

    <solid android:color="@color/primary" />

    <stroke
        android:width="0.5dp"
        android:color="@color/primary"
        android:dashWidth="5dp"
        android:dashGap="5dp" />
</shape>

注意:笔触宽度必须小于线条的高度。在另一种情况下,将不会画线。

答案 2 :(得分:0)

在Android中绘制虚线并不是那么容易。就像你展示的那样可以投掷甚至只是在canwas上绘制虚线(canvas.drawLine(..., paintWithDashEffect))并不总是有效(不适用于所有设备)。您可以使用android:layerType="software"或绘制路径。恕我直言,更好的解决方案是根本不绘制虚线(画出直线)。但如果你真的需要虚线,你可以使用@fearless回答或者像这样的想法:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private Paint mPaint;
private int mDividerSize;

public DividerItemDecoration(int dividerSize) {
    mDividerSize = dividerSize;
    mPaint = new Paint();
    mPaint.setColor(ContextCompat.getColor(context, R.color.colorAccent));
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(dividerSize);
    mPaint.setPathEffect(new DashPathEffect(new float[]{dashGap,dashWidth},0));
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.bottom = mDividerSize;
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    Path path = new Path();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin + mDividerSize/2;

        path.moveTo(left, top);
        path.lineTo(right, top);
    }
    c.drawPath(path, mPaint);
}

}