最近遇到一个问题,用RecyclerVIEw显示数据,纵向列表显示,添加默认分割线。
问题是:底部也会显示分割线,这很影响美观。
怎么解决这个问题呢?我想了很多办法,毫无头绪。。。
最后,查看默认分割线的类divIDerItemdecoration的源码:
public class divIDerItemdecoration extends Itemdecoration { private static final int[] ATTRS = new int[]{16843284}; public static final int HORIZONTAL_List = 0; public static final int VERTICAL_List = 1; private Drawable mdivIDer; private int mOrIEntation; public divIDerItemdecoration(Context context,int orIEntation) { TypedArray a = context.obtainStyledAttributes(ATTRS); this.mdivIDer = a.getDrawable(0); a.recycle(); this.setorIEntation(orIEntation); } public voID setorIEntation(int orIEntation) { if(orIEntation != 0 && orIEntation != 1) { throw new IllegalArgumentException("invalID orIEntation"); } else { this.mOrIEntation = orIEntation; } } public voID onDraw(Canvas c,RecyclerVIEw parent) { if(this.mOrIEntation == 1) { this.drawVertical(c,parent); } else { this.drawHorizontal(c,parent); } } public voID drawVertical(Canvas c,RecyclerVIEw parent) { int left = parent.getpaddingleft(); int right = parent.getWIDth() - parent.getpaddingRight(); int childCount = parent.getChildCount(); for(int i = 0; i < childCount; ++i) { VIEw child = parent.getChildAt(i); LayoutParams params = (LayoutParams)child.getLayoutParams(); int top = child.getBottom() + params.bottommargin; int bottom = top + this.mdivIDer.getIntrinsicHeight(); this.mdivIDer.setBounds(left,top,right,bottom); this.mdivIDer.draw(c); } } public voID drawHorizontal(Canvas c,RecyclerVIEw parent) { int top = parent.getpaddingtop(); int bottom = parent.getHeight() - parent.getpaddingBottom(); int childCount = parent.getChildCount(); for(int i = 0; i < childCount; ++i) { VIEw child = parent.getChildAt(i); LayoutParams params = (LayoutParams)child.getLayoutParams(); int left = child.getRight() + params.rightmargin; int right = left + this.mdivIDer.getIntrinsicHeight(); this.mdivIDer.setBounds(left,bottom); this.mdivIDer.draw(c); } } public voID getItemOffsets(Rect outRect,int itemposition,RecyclerVIEw parent) { if(this.mOrIEntation == 1) { outRect.set(0,this.mdivIDer.getIntrinsicHeight()); } else { outRect.set(0,this.mdivIDer.getIntrinsicWIDth(),0); } }}
因为我用到的是垂直列表,用到的是红色字体处的代码:
public voID drawVertical(Canvas c,bottom); this.mdivIDer.draw(c); } }
从代码中很容易看出只要修改for循环中的内容就可去掉底部的分割线:
public voID drawVertical(Canvas c,RecyclerVIEw parent) { int left = parent.getpaddingleft(); int right = parent.getWIDth() - parent.getpaddingRight(); int childCount = parent.getChildCount(); for(int i = 0; i < childCount-1; ++i) { VIEw child = parent.getChildAt(i); LayoutParams params = (LayoutParams)child.getLayoutParams(); int top = child.getBottom() + params.bottommargin; int bottom = top + this.mdivIDer.getIntrinsicHeight(); this.mdivIDer.setBounds(left,bottom); this.mdivIDer.draw(c); } }
因为这个类我们不能直接修改,所以我们可以自定义一个类,修改相应内容,
添加分割线的时候,使用自定义类。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的RecyclerView消除底部分割线的方法全部内容,希望文章能够帮你解决RecyclerView消除底部分割线的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)