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

Posted

技术标签:

【中文标题】Android - 如何为 RecyclerView 添加点划线分隔线?【英文标题】:Android - How to add a dashed/dotted separator line for RecyclerView? 【发布时间】:2017-02-01 16:38:22 【问题描述】:

我使用this answer 中的代码为我的RecyclerViews 创建了一个实心分隔线。

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

我已经有一个 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: />

</shape>

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

如何解决所以显示虚线?

【问题讨论】:

【参考方案1】:

只需将您的可绘制资源添加到此项目装饰器中即可。

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

和 DividerItemDecorator 类:

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"

在recyclerView的xml中添加这个参数 还将尺寸添加到您的可绘制形状中:

<size android:/>

【讨论】:

你有没有像@ban-geoengineering 显示的那样用 datted drawable 测试它?真的有用吗? @Roman_Donchenko 检查更新,此更改确实有效。 它对我有用 - 有一个变化:我必须将 &lt;size android:height="2dp"/&gt; 添加到我的 line_dashed.xml 才能看到它 - 即,我必须指定一个高度2dp 而不是 1dp。任何想法为什么? 我也必须指定高度为 2dp 而不是 1dp 才能完成这项工作。有什么想法吗? 是的,它在高度为2dp 而不是1dp 时工作。【参考方案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: />

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

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

注意:笔划宽度必须小于线高。在另一种情况下,线不会被绘制。

【讨论】:

这点对我来说很关键:``` 注意:笔划宽度必须小于线的高度。在另一种情况下,将不绘制线。 ```【参考方案3】:

在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);



【讨论】:

以上是关于Android - 如何为 RecyclerView 添加点划线分隔线?的主要内容,如果未能解决你的问题,请参考以下文章

如何为 Android 升级 ProGuard?

Android:如何为参数变量设置默认值

如何为 android 构建 mupdf

Android:如何为 Nexus10 创建 Android 模拟器?

如何为 android 构建 boost::locale

如何为 Nativescript 捆绑运行 android