RecyclerView的万能分割线
程序员文章站
2024-03-04 20:09:54
什么是recyclerview
recyclerview是android 5.0 materials design中的组件之一,相应的还有cardview、palet...
什么是recyclerview
recyclerview是android 5.0 materials design中的组件之一,相应的还有cardview、palette等。看名字我们就能看出一点端倪,没错,它主要的特点就是复用。我们知道,listview中的adapter中可以实现viewholder的复用。recyclerview提供了一个耦合度更低的方式来复用viewholder,并且可以轻松的实现listview、gridview以及瀑布流的效果。
先给大家展示下效果图:
使用方法:
添加默认分割线:高度为2px,颜色为灰色
mrecyclerview.additemdecoration(new recycleviewdivider(mcontext, linearlayoutmanager.vertical));
添加自定义分割线:可自定义分割线drawable
mrecyclerview.additemdecoration(new recycleviewdivider( mcontext, linearlayoutmanager.vertical, r.drawable.divider_mileage));
添加自定义分割线:可自定义分割线高度和颜色
mrecyclerview.additemdecoration(new recycleviewdivider( mcontext, linearlayoutmanager.vertical, 10, getresources().getcolor(r.color.divide_gray_color)));
万能分割线登场:
public class recycleviewdivider extends recyclerview.itemdecoration { private paint mpaint; private drawable mdivider; private int mdividerheight = 2;//分割线高度,默认为1px private int morientation;//列表的方向:linearlayoutmanager.vertical或linearlayoutmanager.horizontal private static final int[] attrs = new int[]{android.r.attr.listdivider}; /** * 默认分割线:高度为2px,颜色为灰色 * * @param context * @param orientation 列表方向 */ public recycleviewdivider(context context, int orientation) { if (orientation != linearlayoutmanager.vertical && orientation != linearlayoutmanager.horizontal) { throw new illegalargumentexception("请输入正确的参数!"); } morientation = orientation; final typedarray a = context.obtainstyledattributes(attrs); mdivider = a.getdrawable(0); a.recycle(); } /** * 自定义分割线 * * @param context * @param orientation 列表方向 * @param drawableid 分割线图片 */ public recycleviewdivider(context context, int orientation, int drawableid) { this(context, orientation); mdivider = contextcompat.getdrawable(context, drawableid); mdividerheight = mdivider.getintrinsicheight(); } /** * 自定义分割线 * * @param context * @param orientation 列表方向 * @param dividerheight 分割线高度 * @param dividercolor 分割线颜色 */ public recycleviewdivider(context context, int orientation, int dividerheight, int dividercolor) { this(context, orientation); mdividerheight = dividerheight; mpaint = new paint(paint.anti_alias_flag); mpaint.setcolor(dividercolor); mpaint.setstyle(paint.style.fill); } //获取分割线尺寸 @override public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) { super.getitemoffsets(outrect, view, parent, state); outrect.set(0, 0, 0, mdividerheight); } //绘制分割线 @override public void ondraw(canvas c, recyclerview parent, recyclerview.state state) { super.ondraw(c, parent, state); if (morientation == linearlayoutmanager.vertical) { drawvertical(c, parent); } else { drawhorizontal(c, parent); } } //绘制横向 item 分割线 private void drawhorizontal(canvas canvas, recyclerview parent) { final int left = parent.getpaddingleft(); final int right = parent.getmeasuredwidth() - parent.getpaddingright(); final int childsize = parent.getchildcount(); for (int i = 0; i < childsize; i++) { final view child = parent.getchildat(i); recyclerview.layoutparams layoutparams = (recyclerview.layoutparams) child.getlayoutparams(); final int top = child.getbottom() + layoutparams.bottommargin; final int bottom = top + mdividerheight; if (mdivider != null) { mdivider.setbounds(left, top, right, bottom); mdivider.draw(canvas); } if (mpaint != null) { canvas.drawrect(left, top, right, bottom, mpaint); } } } //绘制纵向 item 分割线 private void drawvertical(canvas canvas, recyclerview parent) { final int top = parent.getpaddingtop(); final int bottom = parent.getmeasuredheight() - parent.getpaddingbottom(); final int childsize = parent.getchildcount(); for (int i = 0; i < childsize; i++) { final view child = parent.getchildat(i); recyclerview.layoutparams layoutparams = (recyclerview.layoutparams) child.getlayoutparams(); final int left = child.getright() + layoutparams.rightmargin; final int right = left + mdividerheight; if (mdivider != null) { mdivider.setbounds(left, top, right, bottom); mdivider.draw(canvas); } if (mpaint != null) { canvas.drawrect(left, top, right, bottom, mpaint); } } } }
以上所述是小编给大家介绍的recyclerview的万能分割线,希望对大家有所帮助
推荐阅读
-
RecyclerView的万能分割线
-
Android RecyclerView艺术般的控件使用完全解析
-
将替代ListView的RecyclerView 的使用详解(一)
-
解决RecyclerView无法onItemClick问题的两种方法
-
Android中RecyclerView布局代替GridView实现类似支付宝的界面
-
Android RecyclerView的Item自定义动画及DefaultItemAnimator源码分析
-
Android使用recyclerview打造真正的下拉刷新上拉加载效果
-
Android中的RecyclerView新组件初步上手指南
-
Android App使用RecyclerView实现上拉和下拉刷新的方法
-
深入解析Android中的RecyclerView组件