概述
前文有讲RecyclerView的分割线问题,并给出了一款实用的线性布局分割线:http://blog.csdn.net/qq_34995257/article/details/70212797
但我知道广大基友肯定不会满足的,毕竟没有讲使用GridLayoutManager时的情况。所以兄弟我决定再使把力,参照各路资料,把这一种情况的divider封装类也整理出来,只求大家能够喜欢。
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class GridDivider extends RecyclerView.ItemDecoration {
private int spacing;
private boolean isIncludeEdge;
private int vertivalSpacing;
private Drawable mDivider;
public GridDivider(int spacing) {
this.spacing = spacing;
this.isIncludeEdge = false;
this.vertivalSpacing = spacing;
}
public GridDivider(int spacing, int color) {
this.spacing = spacing;
this.isIncludeEdge = false;
this.vertivalSpacing = spacing;
this.mDivider = new ColorDrawable(color);
}
public GridDivider(int spacing, boolean isIncludeEdge) {
this.spacing = spacing;
this.isIncludeEdge = isIncludeEdge;
this.vertivalSpacing = spacing;
}
public GridDivider(int spacing, int vertivalSpacing, boolean isIncludeEdge) {
this.spacing = spacing;
this.isIncludeEdge = isIncludeEdge;
this.vertivalSpacing = vertivalSpacing;
}
public GridDivider(int spacing, int vertivalSpacing, int color, boolean isIncludeEdge) {
this.spacing = spacing;
this.isIncludeEdge = isIncludeEdge;
this.vertivalSpacing = vertivalSpacing;
this.mDivider = new ColorDrawable(color);
}
public void setDivider(Drawable divider) {
this.mDivider = divider;
}
public void setColorDivider(int color) {
this.mDivider = new ColorDrawable(color);
}
public void setSpacing(int spacing) {
this.spacing = spacing;
}
public void setVertivalSpacing(int vertivalSpacing) {
this.vertivalSpacing = vertivalSpacing;
}
public void setIncludeEdge(boolean isIncludeEdge) {
this.isIncludeEdge = isIncludeEdge;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mDivider != null) {
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;
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
final int childCount = parent.getChildCount();
final int spanCount = getSpanCount(parent);
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;
if (!isIncludeEdge && i != 0 && (i + 1) % spanCount == 0) {
right = child.getRight() + params.rightMargin;
} else {
right = child.getRight() + params.rightMargin + spacing;
}
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + vertivalSpacing;
final int lastRaw = childCount % spanCount == 0 ? childCount - spanCount : childCount - childCount % spanCount;
if (isIncludeEdge || i < lastRaw) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
if (isIncludeEdge && i < spanCount) {
final int edgeTop = child.getTop() - params.topMargin - vertivalSpacing;
final int edgeBottom = child.getTop() - params.topMargin;
final int edgeLeft = i == 0 ? left - spacing : left;
mDivider.setBounds(edgeLeft, edgeTop, right, edgeBottom);
mDivider.draw(c);
}
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
final int childCount = parent.getChildCount();
final int spanCount = getSpanCount(parent);
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 + spacing;
if (isIncludeEdge || i == 0 || (i + 1) % spanCount != 0) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
if (isIncludeEdge && i % spanCount == 0) {
final int edgeBottom = child.getBottom() + params.bottomMargin + vertivalSpacing;
final int edgeLeft = child.getLeft() - params.leftMargin - spacing;
final int edgeRight = child.getLeft() - params.leftMargin;
mDivider.setBounds(edgeLeft, top, edgeRight, edgeBottom);
mDivider.draw(c);
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int spanCount = getSpanCount(parent);
int column = position % spanCount; // item column
if (isIncludeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = vertivalSpacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = vertivalSpacing; // item top
}
}
}
}
这次的divider可是很灵活的,同样是通过几种不同的构造方法来创建,然后通过addItemDecoration的方式使用。为了应付产品经理各种各样的需求,这次可以通过传入不同的spacing和vertivalSpacing来定制网格布局中横向和纵向宽度不同的分割线。当然,还有喜闻乐见的是否包含边界isIncludeEdge,都没问题。如果没有传入color或者drawable,那么就是透明分割线,最简单的就在构造方法中只传一个spacing,即可创建横纵向宽度为spacing的透明分割线了,非常灵活。
至于其他需求的分割线,可以参照http://blog.csdn.net/lmj623565791/article/details/45059587这篇文章,定制的关键就在于getItemOffsets和ondraw方法,兄弟只能帮到这里了,剩下的靠你们自己了。
最后
以上就是老实时光为你收集整理的一款灵活的RecyclerView的网格布局分割线的全部内容,希望文章能够帮你解决一款灵活的RecyclerView的网格布局分割线所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复