RecyclerView添加虚线分割线

先看下效果图吧

有些同学觉得这不是很简单吗,随便百度一大堆(好吧你赢了,我只是来做笔记的。。。)

简单粗暴上关键代码

mAdapter.setData(data);
mGrideLayout.setAdapter(mAdapter);
mGrideLayout.setLayoutManager(new GridLayoutManager(this, 2));
mGrideLayout.addItemDecoration(new DividerGridItemDecoration(this));

关键这个DividerGridItemDecoration

//绘制水平虚线
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.GRAY);
Path path = new Path();
path.moveTo(left, top);
path.lineTo(right,top);
PathEffect effects = new DashPathEffect(new float[]{8,8,8,8},0); mPaint.setPathEffect(effects);
c.drawPath(path, mPaint);
//绘制垂直虚线 
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.GRAY);
Path path = new Path();
path.moveTo(left, top);
path.lineTo(left,bottom);
PathEffect effects = new DashPathEffect(new float[]{8,8,8,8},0); mPaint.setPathEffect(effects);
c.drawPath(path, mPaint);

好了就这样了?不,我想说我碰到的一些坑,分享给各位同学

问题一:


What?右边怎么多出来了一条线,我想说这个类DividerGridItemDecoration我没动过

好吧上代码:

修改前;

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_gride"
        android:paddingBottom="200dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

修改后:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_gride"
        android:paddingBottom="200dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

应该是在父布局的边界才隐藏,在来看看美团的,也是这个细节问题(截图不明显)

问题二:

我现在recyclerview中设置了paddingbottom,然后就。。。估计这就是paint的神奇之处吧

参考:http://blog.csdn.net/lmj623565791/article/details/45059587

            http://www.jianshu.com/p/fe41428ca2f3


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值