自定义RecyclerView ItemDecoration

一、实现LinearLayoutManager下的ItemDecoration的自定义

核心就是重写Itemdecoration下的 getItemOffsets方法和onDraw方法

1、getItemOffsets方法主要控制itemview的边距代码实现如下

@Override

void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) parent.getLayoutManager();

  if (linearLayoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {

if (parent.getChildAdapterPosition(view) !=0) {

outRect.top =topBottom;

    }

outRect.left =leftRight;

  }else {

if (parent.getChildAdapterPosition(view) !=0) {

outRect.left =leftRight;

    }

outRect.top =topBottom;

  }

}

2、onDraw方法负责在空白区域绘制(该方法绘制的内容在itemview的下方 注意避免过度绘制)代码实现如下

@Override

void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

int left =0;

  int right =0;

  int top =0;

  int bottom =0;

  if (mColor ==0 || parent.getChildCount() ==0) {

return;

  }

LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();

  if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {

for (int i =0; i < parent.getChildCount(); i++) {

View itemView = parent.getChildAt(i);

      left = parent.getLeft() + parent.getPaddingLeft();

      right = parent.getRight() - parent.getPaddingRight();

      top = itemView.getTop() -topBottom;

      bottom = itemView.getTop();

      if (parent.getChildAdapterPosition(itemView) !=0) {

c.drawRect(left, top, right, bottom, mPaint);

      }

}

}else {

for (int i =0; i < parent.getChildCount(); i++) {

View itemView = parent.getChildAt(i);

      left = itemView.getLeft() -leftRight;

      right = itemView.getLeft();

      top = parent.getTop() + parent.getPaddingTop();

      bottom = parent.getBottom() - parent.getPaddingBottom();

      if (parent.getChildAdapterPosition(itemView) !=0) {

c.drawRect(left, top, right, bottom, mPaint);

      }

}

}

}

二、 实现GridLayoutManager下的ItemDecoration的自定义

@Override

void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

int left;

  int top;

  int right;

  int bottom;

  GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();

  int childCount = layoutManager.getChildCount();

    for (int i =0; i < childCount; i++) {

View child = parent.getChildAt(i);

        left = child.getLeft();

        top = child.getBottom();

        right = child.getRight();

        bottom = top +topBottom;

        c.drawRect(left, top, right, bottom, mPaint);

        left = child.getRight();

        top = child.getTop();

        right = left +leftRight;

        bottom = child.getBottom() +topBottom;

      c.drawRect(left, top, right, bottom, mPaint);

    }

}

@Override

void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();

  GridLayoutManager.LayoutParams layoutParams =

(GridLayoutManager.LayoutParams) view.getLayoutParams();

  int childPosition = parent.getChildAdapterPosition(view);

  totalSpans =totalSpans + layoutParams.getSpanSize();

  if (layoutManager.getOrientation() == GridLayoutManager.VERTICAL) {

if (layoutParams.getSpanIndex() !=0) {

outRect.left =leftRight;

    }

if (childPosition > layoutManager.getSpanCount() -1) {

outRect.top =topBottom;

    }

}else {

if (childPosition > layoutManager.getSpanCount() -1) {

outRect.left =leftRight;

    }

outRect.bottom =topBottom;

  }

}



参考链接: 小甜点,RecyclerView 之 ItemDecoration 讲解及高级特性实践 - frank 的专栏 - CSDN博客

你可能感兴趣的:(自定义RecyclerView ItemDecoration)