RecyclerView 分割线 九宫格

个人建议使用UiAutomator等等工具看看微信、QQ空间、微博的九宫格,最好自己写,不要用RecyclerView或ListView嵌套

importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.graphics.drawable.Drawable;
importandroid.support.v4.content.ContextCompat;
importandroid.support.v7.widget.GridLayoutManager;
importandroid.support.v7.widget.RecyclerView;
importandroid.support.v7.widget.StaggeredGridLayoutManager;
importandroid.view.View;
importandroid.support.v7.widget.RecyclerView.LayoutManager;
importandroid.support.v7.widget.RecyclerView.State;
/**
*@Authorxiaoyao
*@[email protected]
*@ExplainTODO九宫格分界线
*@Createdby Administrator on 2017/8/11 0011 15:36
*/
public classMyListItemDecorationextendsRecyclerView.ItemDecoration {
private static final int[]ATTRS=new int[]{android.R.attr.listDivider};
privateDrawablemDivider;
privatePaintmPaint;
private intmDividerHeight=2;
publicMyListItemDecoration(Context context) {
finalTypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider= a.getDrawable(0);
a.recycle();
}
/**
*自定义分割线
*
*@paramcontext
*@paramdrawableId分割线图片
*/
publicMyListItemDecoration(Context context, intdrawableId) {
mDivider= ContextCompat.getDrawable(context,drawableId);
mDividerHeight=mDivider.getIntrinsicHeight();
}
/**
*自定义分割线
*
*@paramcontext
*@paramdividerHeight分割线高度
*@paramdividerColor分割线颜色
*/
publicMyListItemDecoration(Context context, intdividerHeight, intdividerColor) {
mDividerHeight= dividerHeight;
mPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(dividerColor);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
public voidonDraw(Canvas c,RecyclerView parent,State state) {
drawHorizontal(c,parent);
drawVertical(c,parent);
}
private intgetSpanCount(RecyclerView parent) {
//列数
intspanCount = -1;
LayoutManager layoutManager = parent.getLayoutManager();
if(layoutManagerinstanceofGridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
}else if(layoutManagerinstanceofStaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
returnspanCount;
}
//绘制水平线
public voiddrawHorizontal(Canvas c,RecyclerView parent) {
intchildCount = parent.getChildCount();
for(inti =0;i < childCount;i++) {
finalView child = parent.getChildAt(i);
finalRecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final intleft = child.getLeft() - params.leftMargin;
final intright = child.getRight() + params.rightMargin+mDividerHeight;
final inttop = child.getBottom() + params.bottomMargin;
final intbottom = top +mDividerHeight;
if(mDivider!=null) {
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
}
if(mPaint!=null) {
c.drawRect(left,top,right,bottom,mPaint);
}
}
}
//绘制垂直线
public voiddrawVertical(Canvas c,RecyclerView parent) {
final intchildCount = parent.getChildCount();
for(inti =0;i < childCount;i++) {
finalView child = parent.getChildAt(i);
finalRecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final inttop = child.getTop() - params.topMargin;
final intbottom = child.getBottom() + params.bottomMargin;
final intleft = child.getRight() + params.rightMargin;
final intright = left +mDividerHeight;
if(mDivider!=null) {
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
}
if(mPaint!=null) {
c.drawRect(left,top,right,bottom,mPaint);
}
}
}
//判断是否是最后一列
private booleanisLastColum(RecyclerView parent, intpos, intspanCount, intchildCount) {
LayoutManager layoutManager = parent.getLayoutManager();
if(layoutManagerinstanceofGridLayoutManager) {
if((pos +1) % spanCount ==0)//如果是最后一列,则不需要绘制右边
{
return true;
}
}else if(layoutManagerinstanceofStaggeredGridLayoutManager) {
intorientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
if(orientation == StaggeredGridLayoutManager.VERTICAL) {
if((pos +1) % spanCount ==0) {
//如果是最后一列,则不需要绘制右边
return true;
}
}else{
childCount = childCount - childCount % spanCount;
if(pos >= childCount)//如果是最后一列,则不需要绘制右边
return true;
}
}
return false;
}
//判断是否是最后一行
private booleanisLastRaw(RecyclerView parent, intpos, intspanCount, intchildCount) {
LayoutManager layoutManager = parent.getLayoutManager();
if(layoutManagerinstanceofGridLayoutManager) {
intcC = childCount % spanCount;
if(cC ==0) {
cC = childCount - spanCount;
}else{
cC = childCount - cC;
}
//            childCount = childCount - childCount % spanCount;
if(pos >= cC)//如果是最后一行,则不需要绘制底部
return true;
}else if(layoutManagerinstanceofStaggeredGridLayoutManager) {
intorientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
// StaggeredGridLayoutManager且纵向滚动
if(orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = childCount - childCount % spanCount;
//如果是最后一行,则不需要绘制底部
if(pos >= childCount || childCount == spanCount)
return true;
}else
// StaggeredGridLayoutManager且横向滚动
{
//如果是最后一行,则不需要绘制底部
if((pos +1) % spanCount ==0) {
return true;
}
}
}
return false;
}
@Override
public voidgetItemOffsets(Rect outRect, intitemPosition,RecyclerView parent) {
intspanCount = getSpanCount(parent);
intchildCount = parent.getAdapter().getItemCount();
if(isLastRaw(parent,itemPosition,spanCount,childCount))//如果是最后一行,则不需要绘制底部
{
outRect.set(0,0,mDividerHeight,0);
}else{
}
if(isLastRaw(parent,itemPosition,spanCount,childCount)) {
//TODO最后一行
if(isLastColum(parent,itemPosition,spanCount,childCount)) {
//TODO最后一列
outRect.set(0,0,0,0);
}else{
//TODO不是最后一列
outRect.set(0,0,mDividerHeight,0);
}
}else{
//TODO不是 最后一行
if(isLastColum(parent,itemPosition,spanCount,childCount)) {
//TODO最后一列
outRect.set(0,0,0,mDividerHeight);
}else{
//TODO不是最后一列
outRect.set(0,0,mDividerHeight,mDividerHeight);
}
}
}
}


参考资料:http://www.jianshu.com/p/fe41428ca2f3

你可能感兴趣的:(RecyclerView 分割线 九宫格)