android中RecyclerView自定义分割线实现
最近一直在看recyclerview,较之listview它确实是灵活多变,给予开发者更多自定义的空间,比如:需要添加头部和尾部、item的点击事件、自定义的layoutmanager,还有就是下面要说的自定义的分割线。
1、如何理解分割线
经常听到有人说自定义分割线麻烦,为什么不把分割线写到item布局里,这样不是更简单吗?有些情况把分割线写到item布局里是很难达到我们想要的效果,例如recyclerview里的gridlayoutmanager,staggeredgridlayoutmanager和一些自定义的layoutmanager,不同位置的item需要画的分割线并不相同,这时候应用自定义的分割线就能很好的解决这个问题。
2、如何画分割线
网上也有很多关于recyclerview自定义分割线的写法,很多都是通过获取系统属性中的listdivider来添加,在系统中的apptheme中设置,但是如果我有两种风格的分割线,这就尴尬了呀,所以我希望像listview一样能传入一个drawable来设置分割线,所以我们的思路就是最终能像下面这样设置分割线:
rvstore.additemdecoration(new customdecoration(context,customdecoration.vertical_list,r.drawable.divider_love,unithelper.dip2px(this,15)))
3、具体代码实现
由于recyclerview的布局方式多种多样,所以它的分割线也根据布局的不同有所差异,本文只针对linearlayoutmanager线性布局
- 继承自recyclerview.itemdecoration
- 重写getitemoffsets()、 ondraw()方法
现在给出完整的类,代码中关键地方都有注释,就不再一一说明:
public class customdecoration extends recyclerview.itemdecoration { public static final int horizontal_list = linearlayoutmanager.horizontal; public static final int vertical_list = linearlayoutmanager.vertical; private drawable mdivider; private int morientation; /** * 分割线缩进值 */ private int inset; private paint paint; /** * @param context * @param orientation layout的方向 * @param drawable 引入的drawable的id * @param inset 分割线缩进值 */ public customdecoration(context context, int orientation, int drawable, int inset) { mdivider = context.getresources().getdrawable(drawable); this.inset = inset; paint = new paint(); paint.setcolor(context.getresources().getcolor(r.color.white)); paint.setstyle(paint.style.fill); paint.setantialias(true); setorientation(orientation); } public void setorientation(int orientation) { if (orientation != horizontal_list && orientation != vertical_list) { throw new illegalargumentexception("invalid orientation"); } morientation = orientation; } @override public void ondraw(canvas c, recyclerview parent) { if (morientation == vertical_list) { drawvertical(c, parent); } else { drawhorizontal(c, parent); } } private void drawvertical(canvas c, recyclerview parent) { final int left = parent.getpaddingleft(); final int right = parent.getwidth() - parent.getpaddingright(); final int childcount = parent.getchildcount(); //最后一个item不画分割线 for (int i = 0; i < childcount - 1; i++) { final view child = parent.getchildat(i); final recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams(); final int top = child.getbottom() + params.bottommargin; final int bottom = top + mdivider.getintrinsicheight(); if (inset > 0) { c.drawrect(left, top, right, bottom, paint); mdivider.setbounds(left + inset, top, right - inset, bottom); } else { mdivider.setbounds(left, top, right, bottom); } mdivider.draw(c); } } private void drawhorizontal(canvas c, recyclerview parent) { final int top = parent.getpaddingtop(); final int bottom = parent.getheight() - parent.getpaddingbottom(); final int childcount = parent.getchildcount(); for (int i = 0; i < childcount - 1; 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); } } //由于divider也有宽高,每一个item需要向下或者向右偏移 @override public void getitemoffsets(rect outrect, int itemposition, recyclerview parent) { if (morientation == vertical_list) { outrect.set(0, 0, 0, mdivider.getintrinsicheight()); } else { outrect.set(0, 0, mdivider.getintrinsicwidth(), 0); } } }
4、具体怎么用
recyclerview的三部曲
recyclerview.setlayoutmanager(new linearlayoutmanager(this)); recyclerview.additemdecoration(new customdecoration(this, customdecoration.vertical_list, r.drawable.divider_love, unithelper.dip2px(this, 15))); recyclerview.setadapter(adapter);
r.drawable.divider_love
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#cb8589"/> <size android:height="15dp"/> </shape>
对应的效果如下:
我们可以看到明显的缩进效果,设置成零就没有缩进了。
还是看看正常使用中是什么样子吧
对应的 r.drawable.divider_love
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#cd3131"/> <size android:height="1dp"/> </shape>
我们只需要修改下customdecoration中paint的颜色就可以让缩进的颜色和背景色一致了,默认是白色。
paint.setcolor(color.parsecolor("#ecf0f1"));
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读