万能RecyclerView分割线
就不多叙述了,直接上代码
importandroid.content.Context; importandroid.graphics.Canvas; importandroid.graphics.Paint; importandroid.graphics.Rect; importandroid.support.v7.widget.LinearLayoutManager; importandroid.support.v7.widget.RecyclerView; importandroid.util.TypedValue; importandroid.view.View; publicclassDividerItemDecorationextendsRecyclerView.ItemDecoration{ /* *RecyclerView的布局方向,默认先赋值 *为纵向布局 *RecyclerView布局可横向,也可纵向 *横向和纵向对应的分割想画法不一样 **/ privateintmOrientation=LinearLayoutManager.VERTICAL; /** *item之间分割线的size,1---5 */ privateintmSize; /** *绘制item分割线的画笔,和设置其属性 *来绘制个性分割线 */ privatePaintmPaint; /** *构造方法传入布局方向,不可不传 * *@paramcontextcontext *@paramorientation布局方向 *@paramcolor颜色 *@parammItemSizeitem之间分割线的size */ publicDividerItemDecoration(Contextcontext,intorientation,intcolor,intmItemSize){ this.mOrientation=orientation; /* item之间分割线的颜色 */ this.mSize=mItemSize; if(orientation!=LinearLayoutManager.VERTICAL&&orientation!=LinearLayoutManager.HORIZONTAL){ thrownewIllegalArgumentException("LinearLayoutManagererror"); } mSize=(int)TypedValue.applyDimension(mItemSize,TypedValue.COMPLEX_UNIT_DIP,context.getResources().getDisplayMetrics()); mPaint=newPaint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(color); /*设置填充*/ mPaint.setStyle(Paint.Style.FILL); } @Override publicvoidonDraw(Canvasc,RecyclerViewparent,RecyclerView.Statestate){ if(mOrientation==LinearLayoutManager.VERTICAL){ drawVertical(c,parent); }else{ drawHorizontal(c,parent); } } /** *绘制纵向item分割线 * *@paramcanvascanvas *@paramparentparent */ privatevoiddrawVertical(Canvascanvas,RecyclerViewparent){ finalintleft=parent.getPaddingLeft(); finalintright=parent.getMeasuredWidth()-parent.getPaddingRight(); finalintchildSize=parent.getChildCount(); for(inti=0;i<childSize;i++){ finalViewchild=parent.getChildAt(i); RecyclerView.LayoutParamslayoutParams=(RecyclerView.LayoutParams)child.getLayoutParams(); finalinttop=child.getBottom()+layoutParams.bottomMargin; finalintbottom=top+mSize; canvas.drawRect(left,top,right,bottom,mPaint); } } /** *绘制横向item分割线 * *@paramcanvascanvas *@paramparentparent */ privatevoiddrawHorizontal(Canvascanvas,RecyclerViewparent){ finalinttop=parent.getPaddingTop(); finalintbottom=parent.getMeasuredHeight()-parent.getPaddingBottom(); finalintchildSize=parent.getChildCount(); for(inti=0;i<childSize;i++){ finalViewchild=parent.getChildAt(i); RecyclerView.LayoutParamslayoutParams=(RecyclerView.LayoutParams)child.getLayoutParams(); finalintleft=child.getRight()+layoutParams.rightMargin; finalintright=left+mSize; canvas.drawRect(left,top,right,bottom,mPaint); } } /** *设置item分割线的size * *@paramoutRectoutRect *@paramviewview *@paramparentparent *@paramstatestate */ @Override publicvoidgetItemOffsets(RectoutRect,Viewview,RecyclerViewparent,RecyclerView.Statestate){ if(mOrientation==LinearLayoutManager.VERTICAL){ outRect.set(0,0,0,mSize); }else{ outRect.set(0,0,mSize,0); } } }
调用的时候这样写:
mRecyclerView.addItemDecoration(newDividerItemDecoration(this,LinearLayoutManager.VERTICAL,Color.RED,5));
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。