- 今天来解决RecyclerView第二个问题,就是分割线的问题:原生的RecyclerView并没有像ListView那样提供默认的分割线,我们需要自己处理分割线
- 处理方式为继承RecyclerView的内部类ItemDecoration类,并且复写getItemOffsets()和onDraw()方法.
- 文章内容包括:
- 效果图
- 从源码分析RecyclerView分割线的实现方式
- 列表和网格分割线的实现
- 网格分割线的边界处理
- 先看对比效果
- 查看源码RecyclerView中分割线的实现和RecyclerView的内部类ItemDecoration类
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild {
public RecyclerView(Context context) {
this(context, null);
}
public RecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
...
}
}
- 可以知道RecyclerView是ViewGroup的子类
-
public static abstract class ItemDecoration {
public void onDraw(Canvas c, RecyclerView parent, State state) {
onDraw(c, parent);
}
public void onDrawOver(Canvas c, RecyclerView parent, State state) {
onDrawOver(c, parent);
}
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
parent);
}
}
-
通过源码的继承类发现:
- ItemDecoration没有默认的分割线实现类,只有一个ItemTouchHelper类继承(这个类后面分析Item交互时会讲解到)
- 该类内部的几个方法的作用(该文章源码等级为24,过时方法不分析):
- getItemOffsets()方法作用是获取每个条目的偏移量,具体值放在参数outRect中,要获取具体的条目位置可通过方法RecyclerView.getChildAdapterPosition(view)获取
- onDraw()的作用是分割线的绘制,在传入的参数Canvas中绘制
- onDrawOver()分割线绘制完调用的方法(一般没用到)
- 现在来分析RecyclerView的源码->找出他是如何来绘制分割线的
- RecyclerView继承自ViewGroup,自然要看他的三个主要方法onMeasure(),onLayout(),onDraw()和ViewGroup独有的方法measureChild().
- 通过查看方法onMeasure()和onLayout()没有发现分割线的影子,而在onDraw()中发现调用了ItemDecoration类的onDraw()方法.
@Override
public void onDraw(Canvas c) {
super.onDraw(c);
final int count = mItemDecorations.size();
for (int i = 0; i < count; i++) {
mItemDecorations.get(i).onDraw(c, this, mState);
}
}
- 那必然在measureChild()方法中,会调用分割线的其他方法
public void measureChild(View child, int widthUsed, int heightUsed) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
widthUsed += insets.left + insets.right;
heightUsed += insets.top + insets.bottom;
final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(),
getPaddingLeft() + getPaddingRight() + widthUsed, lp.width,
canScrollHorizontally());
final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),
getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
canScrollVertically());
if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) {
child.measure(widthSpec, heightSpec);
}
}
- 发现调用了方法getItemDecorInsetsForChild();意思是获取每个childView插入的分割线大小.然后再测量childView
Rect getItemDecorInsetsForChild(View child) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (!lp.mInsetsDirty) {
return lp.mDecorInsets;
}
final Rect insets = lp.mDecorInsets;
insets.set(0, 0, 0, 0);
final int decorCount = mItemDecorations.size();
for (int i = 0; i < decorCount; i++) {
mTempRect.set(0, 0, 0, 0);
mItemDecorations.get(i).getItemOffsets(mTempRect, child, this, mState);
insets.left += mTempRect.left;
insets.top += mTempRect.top;
insets.right += mTempRect.right;
insets.bottom += mTempRect.bottom;
}
lp.mInsetsDirty = false;
return insets;
}
- 分割线偏移量方法getItemOffsets()方法也找到了最终调用的位置
- 了解了RecyclerView的分割线源码实现过程,要实现就不难了
- 列表分割线的代码实现
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
/**
* 垂直方向分割线
* @param c
* @param parent
*/
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
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.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
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 left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
- 网格分割线的代码实现-注意最后一行和最后一列不添加分割线
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {
private String TAG = this.getClass().getSimpleName();
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
private Drawable mDivider;
public DividerGridItemDecoration(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) {
drawVertical(c, parent);
drawHorizontal(c, parent);
}
/**
* 水平方向分割线
* @param c
* @param parent
*/
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
if (isLastRow(parent, i, getSpanCount(parent), childCount))//最后一行不绘制
continue;
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;
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);
if (isLastColumn(parent, i, getSpanCount(parent), childCount))//最后一列不绘制
continue;
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);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int right = mDivider.getIntrinsicWidth();
int bottom = mDivider.getIntrinsicHeight();
outRect.set(0, 0, right, bottom);
}
/**
* 是否是最后一列
*
* @param parent
* @param currChildPos
* @param spanCount
* @param itemCount
* @return
*/
private boolean isLastColumn(RecyclerView parent, int currChildPos, int spanCount, int itemCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager != null && layoutManager instanceof GridLayoutManager) {
if ((currChildPos + 1) % spanCount == 0) {
return true;
}
} else if (layoutManager != null && layoutManager instanceof StaggeredGridLayoutManager) {
//瀑布流也有两种方向
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
int orientation = staggeredGridLayoutManager.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {//垂直
if ((currChildPos + 1) % spanCount == 0) {
return true;
}
} else {
if (itemCount % spanCount != 0) {
itemCount = itemCount - itemCount % spanCount;
if (currChildPos >= itemCount) {//这种判断方式在item最后一行未填满的情况下可行
return true;
}
} else {//最后一行填满的情况处理
itemCount = itemCount - spanCount;
if (currChildPos >= itemCount) {
return true;
}
}
}
}
return false;
}
/**
* 是否是最后一行
*
* @param parent
* @param currChildPos
* @param spanCount
* @param itemCount
* @return
*/
private boolean isLastRow(RecyclerView parent, int currChildPos, int spanCount, int itemCount) {
boolean result = false;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager != null && layoutManager instanceof GridLayoutManager) {
if (itemCount % spanCount != 0) {
itemCount = itemCount - itemCount % spanCount;
if (currChildPos >= itemCount) {//这种判断方式在item最后一行未填满的情况下可行
return true;
}
} else {//最后一行填满的情况处理
itemCount = itemCount - spanCount;
if (currChildPos >= itemCount) {
return true;
}
}
} else if (layoutManager != null && layoutManager instanceof StaggeredGridLayoutManager) {
//瀑布流也有两种方向
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
int orientation = staggeredGridLayoutManager.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {//垂直
if (itemCount % spanCount != 0) {
itemCount = itemCount - itemCount % spanCount;
if (currChildPos >= itemCount) {//这种判断方式在item最后一行未填满的情况下可行
return true;
}
} else {//最后一行填满的情况处理
itemCount = itemCount - spanCount;
if (currChildPos >= itemCount) {
return true;
}
}
} else {
if (currChildPos % spanCount == 0) {
return true;
}
}
}
return false;
}
/**
* 获取当前item在recycler的adapter的位置
*
* @param parent
* @param view
* @return
*/
private int getCurrentChildPosition(RecyclerView parent, View view) {
return parent.getChildAdapterPosition(view);
}
private int getSpanCount(RecyclerView parent) {
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
spanCount = gridLayoutManager.getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
spanCount = staggeredGridLayoutManager.getSpanCount();
}
return spanCount;
}
}
- 总结:在处理最后一行分割线不绘制的时候发现,在偏移量方法getItemOffset()方法中处理并不起效果,为了准确我将判断最后一行和最后一列的childView的分割线放到绘制方法中调用,是最后一行就不绘制.
- 网上的判断最后一行的方法有缺陷,未考虑最后一行是否填满,在未填满时,绘制没有问题,这也反映了很多人的博客都是复制粘贴过来,而没有自己真正考虑过.
作者:Timmy_zzh 发表于2016/12/13 17:12:35 原文链接
阅读:29 评论:0 查看评论