万能RecyclerView分割线

万能RecyclerView分割线,第1张

概述就不多叙述了,直接上代码importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Paint;

就不多叙述了,直接上代码

import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.Rect;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.TypedValue;import androID.vIEw.VIEw;public class divIDerItemdecoration extends RecyclerVIEw.Itemdecoration {  /*  * RecyclerVIEw的布局方向,默认先赋值  * 为纵向布局  * RecyclerVIEw 布局可横向,也可纵向  * 横向和纵向对应的分割想画法不一样  * */  private int mOrIEntation = linearlayoutmanager.VERTICAL;  /**   * item之间分割线的size,1---5   */  private int mSize;  /**   * 绘制item分割线的画笔,和设置其属性   * 来绘制个性分割线   */  private Paint mPaint;  /**   * 构造方法传入布局方向,不可不传   *   * @param context   context   * @param orIEntation 布局方向   * @param color    颜色   * @param mItemSize  item之间分割线的size   */  public divIDerItemdecoration(Context context,int orIEntation,int color,int mItemSize) {    this.mOrIEntation = orIEntation;    /*   item之间分割线的颜色   */    this.mSize= mItemSize;    if (orIEntation != linearlayoutmanager.VERTICAL && orIEntation != linearlayoutmanager.HORIZONTAL) {      throw new IllegalArgumentException("linearlayoutmanager error");    }    mSize = (int) TypedValue.applyDimension(mItemSize,TypedValue.COMPLEX_UNIT_DIP,context.getResources().getdisplayMetrics());    mPaint = new Paint(Paint.ANTI_AliAS_FLAG);    mPaint.setcolor(color);     /*设置填充*/    mPaint.setStyle(Paint.Style.FILL);  }  @OverrIDe  public voID onDraw(Canvas c,RecyclerVIEw parent,RecyclerVIEw.State state) {    if (mOrIEntation == linearlayoutmanager.VERTICAL) {      drawVertical(c,parent);    } else {      drawHorizontal(c,parent);    }  }  /**   * 绘制纵向 item 分割线   *   * @param canvas canvas   * @param parent parent   */  private voID drawVertical(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 + mSize;      canvas.drawRect(left,top,right,bottom,mPaint);    }  }  /**   * 绘制横向 item 分割线   *   * @param canvas canvas   * @param parent parent   */  private voID drawHorizontal(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 + mSize;      canvas.drawRect(left,mPaint);    }  }  /**   * 设置item分割线的size   *   * @param outRect outRect   * @param vIEw  vIEw   * @param parent parent   * @param state  state   */  @OverrIDe  public voID getItemOffsets(Rect outRect,VIEw vIEw,RecyclerVIEw.State state) {    if (mOrIEntation == linearlayoutmanager.VERTICAL) {      outRect.set(0,mSize);    } else {      outRect.set(0,mSize,0);    }  }}

调用的时候这样写:

复制代码 代码如下:mRecyclerVIEw.addItemdecoration(new divIDerItemdecoration(this,linearlayoutmanager.VERTICAL,color.RED,5));

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

总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存