欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

万能RecyclerView分割线

程序员文章站 2024-02-23 22:39:22
就不多叙述了,直接上代码 import android.content.context; import android.graphics.canvas; im...

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

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, top, right, bottom, mpaint);
    }
  }

  /**
   * 设置item分割线的size
   *
   * @param outrect outrect
   * @param view  view
   * @param parent parent
   * @param state  state
   */
  @override
  public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) {
    if (morientation == linearlayoutmanager.vertical) {
      outrect.set(0, 0, 0, msize);
    } else {
      outrect.set(0, 0, msize, 0);
    }
  }
}

调用的时候这样写:

复制代码 代码如下:
mrecyclerview.additemdecoration(new divideritemdecoration(this, linearlayoutmanager.vertical, color.red,5));

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