/**
* Desc:九宫格分割线 无最右边与最下边线
* Author:zhangcx
* Data: 2018/8/1 11:56
*/
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable mDivider;
public GridDividerItemDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
private int getSpanCount(RecyclerView parent) {
// 列数
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
}
return spanCount;
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin
+ mDivider.getIntrinsicWidth();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 是否是最后一行
*/
private boolean isLastRow(int itemPosition, RecyclerView parent) {
int spanCount = getSpanCount(parent);
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//有多少列
if (layoutManager instanceof GridLayoutManager) {
int childCount = parent.getAdapter().getItemCount();
double count = Math.ceil((double) childCount / (double) spanCount);//总行数
double currentCount = Math.ceil((double) (itemPosition + 1) / spanCount);//当前行数
//最后当前数量小于总的
if (currentCount < count) {
return false;
}
}
return true;
}
/**
* 判断是否是最后一列
*/
private boolean isLastColum(int itemPosition, RecyclerView parent) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//有多少列
if (layoutManager instanceof GridLayoutManager) {
int spanCount = getSpanCount(parent);
if ((itemPosition + 1) % spanCount == 0) {//因为是从0可以所以要将ItemPosition先加1
return true;
}
}
return false;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
state) {
super.getItemOffsets(outRect, view, parent, state);
LogUtil.out("++++++++++++++条目," + parent.getChildLayoutPosition(view) + "是否最后一行" + isLastRow(parent.getChildLayoutPosition(view), parent));
LogUtil.out("++++++++++++++条目," + parent.getChildLayoutPosition(view) + "是否最后一列" + isLastColum(parent.getChildLayoutPosition(view), parent));
if (isLastRow(parent.getChildLayoutPosition(view), parent))// 如果是最后一行,则不需要绘制底部
{
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
}
if (isLastColum(parent.getChildLayoutPosition(view), parent))// 如果是最后一列,则不需要绘制右边
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
我们调用mRecyclerView.addItemDecoration()方法添加decoration的时候,RecyclerView在绘制的时候,去会绘制decorator,即调用该类的onDraw和onDrawOver方法,
判断最后一行的方法
①条目总数 /列数 向上取整 拿到总行数
②当前条目位置/ 列数 取到当前位于的行数
③ 当前行数,小于总行数 那就不是最后一行
private boolean isLastRow(int itemPosition, RecyclerView parent) {
int spanCount = getSpanCount(parent);
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//有多少列
if (layoutManager instanceof GridLayoutManager) {
int childCount = parent.getAdapter().getItemCount();
double count = Math.ceil((double) childCount / (double) spanCount);//总行数
double currentCount = Math.ceil((double) (itemPosition + 1) / spanCount);//当前行数
//最后当前数量小于总的
if (currentCount < count) {
return false;
}
}
return true;
}
判断是否为最后一列
当前位置 取余 列数 为0时,就是最后一列
private boolean isLastColum(int itemPosition, RecyclerView parent) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//有多少列
if (layoutManager instanceof GridLayoutManager) {
int spanCount = getSpanCount(parent);
if ((itemPosition + 1) % spanCount == 0) {//因为是从0可以所以要将ItemPosition先加1
return true;
}
}
return false;
}
分配Item偏移量的时对position进行判断,符合则下部和右侧条件则将bottom和right设置偏移量。
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
state) {
super.getItemOffsets(outRect, view, parent, state);
if (isLastRow(parent.getChildLayoutPosition(view), parent))// 如果是最后一行,则不需要绘制底部
{
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
}
if (isLastColum(parent.getChildLayoutPosition(view), parent))// 如果是最后一列,则不需要绘制右边
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}