android中RecyclerView自定义分割线实现

android中RecyclerView自定义分割线实现,第1张

概述最近一直在看RecyclerView,较之ListView它确实是灵活多变,给予开发者更多自定义的空间,比如:需要添加头部和尾部、item的点击事件、自定义的LayoutManager,还有就是下面要说的自定义的分割线

最近一直在看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,right - inset,bottom);     } else {       mdivIDer.setBounds(left,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,bottom);     mdivIDer.draw(c);   } } //由于divIDer也有宽高,每一个Item需要向下或者向右偏移 @OverrIDe public voID getItemOffsets(Rect outRect,int itemposition,RecyclerVIEw parent) {   if (mOrIEntation == VERTICAL_List) {     outRect.set(0,mdivIDer.getIntrinsicHeight());   } else {     outRect.set(0,mdivIDer.getIntrinsicWIDth(),0);   } }}

4、具体怎么用

RecyclerVIEw的三部曲

recyclerVIEw.setLayoutManager(new linearlayoutmanager(this));recyclerVIEw.addItemdecoration(new Customdecoration(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"));

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android中RecyclerView自定义分割线实现全部内容,希望文章能够帮你解决android中RecyclerView自定义分割线实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存