自定义RecycleView的分割线+消除底部的分割线

https://github.com/yqritc/RecyclerView-FlexibleDivider

compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'
mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(getResources().getColor(R.color.fenge)).size(20).build());

消除底部的分割线的方法在 getItemOffsets里面被注释掉了,由于前期UI说最后一个去掉,后又说算了加上,所以你懂得MMP!!!

添加默认分割线:高度为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)));

mRecyclerView.setAdapter(myAdapter);
mRecyclerView.addItemDecoration(new RecycleViewDivider( mContext, LinearLayoutManager.HORIZONTAL, 20, Color.RED));

/**

  • Created by JsonQiu on 2017/10/16 17:31.
    */
    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);

    /*
    设置最后一个item无分割线
    int childAdapterPosition = parent.getChildAdapterPosition(view);
    int lastCount = parent.getAdapter().getItemCount() - 1;
    //如果当前条目与是最后一个条目,就不设置divider padding
    if (childAdapterPosition == lastCount) {
        outRect.set(0, 0, 0, 0);
        return;
    }
    if (mOrientation == LinearLayoutCompat.HORIZONTAL) {
        outRect.set(0, 0, mDividerHeight, 0);
        //0,0,30,0,设置宽度
    } else {
        outRect.set(0, 0, 0, mDividerHeight);
        //0,0,0,30,设置高度
    }*/
}

//绘制分割线
@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);
        }
    }
}

你可能感兴趣的:(自定义RecycleView的分割线+消除底部的分割线)