RecyclerView GridView类型的ItemDecoration

packagecom.lonzh.finace.widget;

importandroid.graphics.Canvas;

importandroid.graphics.Rect;

importandroid.graphics.drawable.Drawable;

importandroid.support.v7.widget.RecyclerView;

importandroid.view.View;

/**

* Created by Administrator on 2017/10/19 0019.

*/

public classGridItemDecorationextendsRecyclerView.ItemDecoration {

privateDrawablemDivider;

private intspanCount;

private intspaceSize;

private booleanincludeLREdge;

private booleanincludeTBEdge;

private booleandrawLREdge;

private booleandrawTBEdge;

privateGridItemDecoration(Builder builder) {

mDivider= builder.mDivider;

spanCount= builder.spanCount;

spaceSize= builder.spaceSize;

includeLREdge= builder.includeLREdge;

includeTBEdge= builder.includeTBEdge;

drawLREdge= builder.drawLREdge;

drawTBEdge= builder.drawTBEdge;

}

@Override

publicStringtoString() {

return"GridItemDecoration{"+

"spanCount="+spanCount+

", spaceSize="+spaceSize+

", includeLREdge="+includeLREdge+

", includeTBEdge="+includeTBEdge+

'}';

}

@Override

public voidgetItemOffsets(Rect outRect,View view,RecyclerView parent,RecyclerView.State state) {

intposition = parent.getChildAdapterPosition(view);// item position

intcolumn = position %spanCount;// item column

if(includeLREdge) {

outRect.left=spaceSize- column *spaceSize/spanCount;

outRect.right= (column +1) *spaceSize/spanCount;

}else{

outRect.left= column *spaceSize/spanCount;

outRect.right=spaceSize- (column +1) *spaceSize/spanCount;

}

if(includeTBEdge) {

if(position

outRect.bottom=spaceSize;// item bottom

}else{

if(position >=spanCount) outRect.top=spaceSize;// item top

}

}

@Override

public voidonDraw(Canvas c,RecyclerView parent,RecyclerView.State state) {

if(mDivider!=null) {

drawHorizontal(c,parent);

drawVertical(c,parent);

if(includeLREdge&&drawLREdge) drawLR(c,parent);

if(includeTBEdge&&drawTBEdge) drawTB(c,parent);

}

}

private voiddrawLR(Canvas c,RecyclerView parent) {

intchildCount = parent.getChildCount();

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

View child = parent.getChildAt(i);

//最左边那条线

if(i %spanCount==0) {

intleft = child.getLeft() -spaceSize;

intright = left +spaceSize;

intbottom = child.getBottom();

inttop = child.getTop() -spaceSize;

if(i ==0) top = child.getTop();//【左上方】那一块交给drawTB绘制

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}

//最右边那条线

if((i +1) %spanCount==0) {

intleft = child.getRight();

intright = left +spaceSize;

intbottom = child.getBottom();

inttop = child.getTop() -spaceSize;

if(i ==spanCount-1) top = child.getTop();//【右上方】那一块交给drawTB绘制

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}

}

}

private voiddrawTB(Canvas c,RecyclerView parent) {

intchildCount = parent.getChildCount();

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

View child = parent.getChildAt(i);

//最上边那条线

if(i

inttop = child.getTop() -spaceSize;

intbottom = top +spaceSize;

intleft = child.getLeft();

intright = child.getRight() +spaceSize;

if((i +1) %spanCount==0||

(childCount

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}

//最下边那条线

if(childCount %spanCount==0&& i >=spanCount* (childCount /spanCount-1)) {

inttop = child.getBottom();

intbottom = top +spaceSize;

intleft = child.getLeft() -spaceSize;

intright = child.getRight();

if((i +1) %spanCount==0) right = child.getRight() +spaceSize;//最右边那条线

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}else if(i >=spanCount* (childCount /spanCount)) {

inttop = child.getBottom();

intbottom = top +spaceSize;

intright = child.getRight();

intleft = child.getLeft() -spaceSize;

if(!drawLREdge&& i %spanCount==0) left = child.getLeft();//最左边那条线

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}

}

}

private voiddrawHorizontal(Canvas c,RecyclerView parent) {

intchildCount = parent.getChildCount();

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

if(i >=spanCount) {

View child = parent.getChildAt(i);

intleft = child.getLeft() -spaceSize;

if(i %spanCount==0) left = child.getLeft();

intright = child.getRight();

inttop = child.getTop() -spaceSize;

intbottom = top +spaceSize;

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}

}

}

private voiddrawVertical(Canvas c,RecyclerView parent) {

intchildCount = parent.getChildCount();

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

if(i %spanCount!=0) {

View child = parent.getChildAt(i);

inttop = child.getTop();

intbottom = child.getBottom();

intleft = child.getLeft() -spaceSize;

intright = left +spaceSize;

mDivider.setBounds(left,top,right,bottom);

mDivider.draw(c);

}

}

}

public static final classBuilder {

privateDrawablemDivider=null;

private intspanCount;

private intspaceSize;

private booleanincludeLREdge=false;

private booleanincludeTBEdge=false;

private booleandrawLREdge=true;

private booleandrawTBEdge=true;

publicBuilder() {

}

/**

* 若不指定,则使用空白代替

*/

publicBuildermDivider(Drawable val) {

mDivider= val;

return this;

}

/**

* 行数或列数

*/

publicBuilderspanCount(intval) {

spanCount= val;

return this;

}

/**

* 行列间距大小

*/

publicBuilderspaceSize(intval) {

spaceSize= val;

return this;

}

/**

* 是否包含左右边界

*/

publicBuilderincludeLREdge(booleanval) {

includeLREdge= val;

return this;

}

/**

* 是否包含上下边界

*/

publicBuilderincludeTBEdge(booleanval) {

includeTBEdge= val;

return this;

}

/**

* 是否绘制左右边界

*/

publicBuilderdrawLREdge(booleanval) {

drawLREdge= val;

return this;

}

/**

* 是否绘制上下边界

*/

publicBuilderdrawTBEdge(booleanval) {

drawTBEdge= val;

return this;

}

publicGridItemDecorationbuild() {

return newGridItemDecoration(this);

}

}

}


//使用

public voidsetItemDecoration(intsize, intcount) {

GridItemDecoration.Builder builder =newGridItemDecoration.Builder();

builder.mDivider(_mActivity.getResources().getDrawable(R.color.sy_gray))

.spaceSize(size)

.drawLREdge(false)

.drawLREdge(false)

.includeLREdge(false)

.includeTBEdge(false)

.spanCount(count)

.includeLREdge(false)

.includeTBEdge(false)

.includeTBEdge(false)

.drawTBEdge(false);

lpRecyclerView.addItemDecoration(builder.build());

}

你可能感兴趣的:(RecyclerView GridView类型的ItemDecoration)