AndroidRecyclerView的网格布局ItemDecoration

更新时间:2023-07-13 12:58:24 阅读: 评论:0

添加isHeadDivider控制headview是否显⽰divider
删除最右侧边线
适⽤于GridLayoutManager
⽤于StaggeredGridLayoutManager时,由于Item显⽰位置不受控制,divider请适⽤背景颜⾊,有时间我会上图⽚说明附上完整代码
package com.inba.lightempty.support.view.customRecycleView;
t.Context;
aphics.Canvas;
aphics.Paint;
aphics.Rect;
import android.support.annotation.ColorRes;
import android.support.annotation.Px;如何考研
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
/**
* Created by Mark on 2017/6/2.
*/
public class GridItemDecoration extends ItemDecoration {
Paint mVerPaint, mHorPaint;
Builder mBuilder;
public GridItemDecoration(Builder builder) {
init(builder);
}
void init(Builder builder) {
this.mBuilder = builder;
mVerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mVerPaint.tStyle(Paint.Style.FILL);
period是什么意思mVerPaint.tColor(builder.verColor);
mHorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHorPaint.tStyle(Paint.Style.FILL);
mHorPaint.tColor(builder.horColor);
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = ChildCount();
for (int i = 0; i < childCount; i++) {
View child = ChildAt(i);
//is draw head divider
//version 1.1 update
ChildViewHolder(child).getItemViewType() == 1&&!mBuilder.isShowHeadDivider)
continue;
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) LayoutParams();
final int left = Left() - params.leftMargin;
final int right = Right() + params.rightMargin;
wholelife
final int top = Bottom() + params.bottomMargin;
final int bottom = top + mBuilder.dividerHorSize;
c.drawRect(left, top, right, bottom, mHorPaint);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
final int childCount = ChildCount();
for (int i = 0; i < childCount; i++) {
final View child = ChildAt(i);
final View child = ChildAt(i);
/
五年级英语视频/remove the rightmost divider
//verion 1.1 update
if((ChildViewHolder(child).getAdapterPosition())%getSpanCount(parent)==0)
continue;
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = Top() - pMargin;
final int bottom = Bottom() + params.bottomMargin+mBuilder.dividerHorSize;
final int left = Right() + params.rightMargin;
final int right = left + mBuilder.dividerVerSize;
c.drawRect(left, top, right, bottom, mVerPaint);
英孚教育价格}
}
private int getSpanCount(RecyclerView parent) {
// 列数
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = LayoutManager();
high maintenanceif (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} el if (layoutManager instanceof StaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager)
.getSpanCount();
}
return spanCount;
}
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
int childCount) {
RecyclerView.LayoutManager layoutManager = LayoutManager();
if (layoutManager instanceof GridLayoutManager) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)
return true;
} el if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
猛犸象的圣诞节
.getOrientation();
// StaggeredGridLayoutManager 且纵向滚动
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)
return true;
} el
// StaggeredGridLayoutManager 且横向滚动
{
if ((pos + 1) % spanCount == 0) {
return true;
}
}
}
return fal;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
@Override
public void getItemOffts(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        ItemOffts(outRect, view, parent, state);
快速有效的美白方法int spanCount = getSpanCount(parent);
int childCount = Adapter().getItemCount();
int itemPosition = ((RecyclerView.LayoutParams) LayoutParams()).getViewLayoutPosition();
int itemPosition = ((RecyclerView.LayoutParams) LayoutParams()).getViewLayoutPosition();        if (mBuilder.isExistHeadView)
itemPosition -= 1;
//t head divider offt
//version 1.1 update
if(mBuilder.isShowHeadDivider&&itemPosition==-1)
outRect.t(0, 0, 0, mBuilder.dividerHorSize);
if (itemPosition < 0)
return;
int column = itemPosition % spanCount;
int bottom = 0;
int left = column * mBuilder.dividerVerSize / spanCount;
int right = mBuilder.dividerVerSize - (column + 1) * mBuilder.dividerVerSize / spanCount;
if (!(isLastRaw(parent, itemPosition, spanCount, childCount) && !mBuilder.isShowLastDivider))
bottom = mBuilder.dividerHorSize;
outRect.t(left, 0, right, bottom);
marginOffts(outRect, spanCount, itemPosition);
}
private void marginOffts(Rect outRect, int spanCount, int itemPosition) {
if (mBuilder.marginRight == 0 && mBuilder.marginLeft == 0)
return;
int itemShrink = (mBuilder.marginLeft + mBuilder.marginRight) / spanCount;
outRect.left += (mBuilder.marginLeft - (itemPosition % spanCount) * itemShrink);
outRect.right += ((itemPosition % spanCount) + 1) * itemShrink - mBuilder.marginLeft;
}
public static class Builder {
private Context c;
int horColor;
int verColor;
int dividerHorSize;
int dividerVerSize;
int marginLeft, marginRight;visin
boolean isShowLastDivider = fal;
boolean isExistHeadView = fal;
boolean isShowHeadDivider = fal;
public Builder(Context c) {
this.c = c;
}
/
**
* 设置divider的颜⾊
*
* @param color
* @return
*/
public Builder color(@ColorRes int color) {
this.horColor = c.getResources().getColor(color);
this.verColor = c.getResources().getColor(color);
return this;
}
/
**
* 单独设置横向divider的颜⾊
*
* @param horColor
* @return
*/
public Builder horColor(@ColorRes int horColor) {
this.horColor = c.getResources().getColor(horColor);
return this;
}
/**
* 单独设置纵向divider的颜⾊
*
* @param verColor
* @return
*/
public Builder verColor(@ColorRes int verColor) {
this.verColor = c.getResources().getColor(verColor);
return this;
}
/**
* 设置divider的宽度
*
* @param size
* @return
*/
public Builder size(@Px int size) {
this.dividerHorSize = size;
this.dividerVerSize = size;
return this;
}
/**
* 设置横向divider的宽度
*
* @param horSize
* @return
*/
public Builder horSize(@Px int horSize) {
this.dividerHorSize = horSize;
return this;
}
/**
* 设置纵向divider的宽度
*
* @param verSize
* @return
*/
public Builder verSize(@Px int verSize) {
this.dividerVerSize = verSize;
return this;
}
/**
* 设置剔除HeadView的RecyclerView左右两边的外间距
*
* @param marginLeft
* @param marginRight
* @return
*/
public Builder margin(@Px int marginLeft,@Px int marginRight) {            this.marginLeft = marginLeft;
this.marginRight = marginRight;
return this;
}
/**
* 最后⼀⾏divider是否需要显⽰
*
* @param isShow
* @return
*/
public Builder showLastDivider(boolean isShow) {
this.isShowLastDivider = isShow;
return this;
}
/**
barley* HeadView⾏divider是否需要显⽰
* Version 1.1 add
*

本文发布于:2023-07-13 12:58:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/176143.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   是否   控制   宽度   边线   说明   教育   美白
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图