概述:
當(dāng)我們準(zhǔn)備為RecyclerView添加分割線時(shí),你會(huì)發(fā)現(xiàn)RecyclerView并沒有支持divider這樣的屬性。想想辦法,你可以給Item的布局去設(shè)置margin,當(dāng)然了這種方式不夠優(yōu)雅,我們可以自由的去定制它。
繪制橫向或縱向列表分割線:
RecyclerView.ItemDecoration是一個(gè)抽象類,就是用來裝飾RecyclerView的子item的,通過名字就可以知道,功能并不僅僅是添加間距繪制分割線,是用來裝飾item的。
這個(gè)類包含三個(gè)方法?:
1、getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)//設(shè)置四邊邊距
2、onDraw(Canvas c, RecyclerView parent, State state)//繪制裝飾
3、onDrawOver(Canvas c, RecyclerView parent, State state)//繪制蒙層
1、getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)方法:
該方法中有4個(gè)參數(shù):
1、Rect outRect: outRect 設(shè)置的4個(gè)方向的值,將被計(jì)算進(jìn)所有 decoration 的尺寸中,而這個(gè)尺寸,被用來計(jì)算 RecyclerView 每個(gè) item view 的padding間距大小。
2、View view: childView,就是item,可以理解為item的根View,并不是item中的控件
3、RecyclerView parent:就是RecyclerView自身
4、RecyclerView.State state : RecyclerView的狀態(tài)
代碼實(shí)現(xiàn):
//獲取分割線尺寸
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
? ? super.getItemOffsets(outRect, view, parent, state);
? ? outRect.set(0, 0, 0, mDividerHeight);
}
2、onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)方法:
onDraw用于繪制divider,繪制到每一個(gè)item的下一層,也就是說item會(huì)蓋在divider所在層的上面
onDraw()方法可以為divier設(shè)置繪制范圍,并且繪制范圍可以超出在?getItemOffsets中設(shè)置的范圍,但由于是在item下面一層進(jìn)行繪制,會(huì)存在overdraw
代碼實(shí)現(xiàn):
//繪制分割線
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
? ? super.onDraw(c, parent, state);
? ? if (mOrientation == LinearLayoutManager.VERTICAL) {
? ? ? ? drawVertical(c, parent);
? ? } else {
? ? ? ? drawHorizontal(c, parent);
? ? }
}
//繪制橫向 item 分割線
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
? ? final int left = parent.getPaddingLeft();
? ? final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
? ? final int childSize = parent.getChildCount();
? ? for (int i = 0; i < childSize; i++) {
? ? ? ? final View child = parent.getChildAt(i);
? ? ? ? RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
? ? ? ? final int top = child.getBottom() + layoutParams.bottomMargin;
? ? ? ? final int bottom = top + mDividerHeight;
? ? ? ? if (mDivider != null) {
? ? ? ? ? ? mDivider.setBounds(left, top, right, bottom);
? ? ? ? ? ? mDivider.draw(canvas);
? ? ? ? }
? ? ? ? if (mPaint != null) {
? ? ? ? ? ? canvas.drawRect(left, top, right, bottom, mPaint);
? ? ? ? }
? ? }
}
//繪制縱向 item 分割線
private void drawVertical(Canvas canvas, RecyclerView parent) {
? ? final int top = parent.getPaddingTop();
? ? final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
? ? final int childSize = parent.getChildCount();
? ? for (int i = 0; i < childSize; i++) {
? ? ? ? final View child = parent.getChildAt(i);
? ? ? ? RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
? ? ? ? final int left = child.getRight() + layoutParams.rightMargin;
? ? ? ? final int right = left + mDividerHeight;
? ? ? ? if (mDivider != null) {
? ? ? ? ? ? mDivider.setBounds(left, top, right, bottom);
? ? ? ? ? ? mDivider.draw(canvas);
? ? ? ? }
? ? ? ? if (mPaint != null) {
? ? ? ? ? ? canvas.drawRect(left, top, right, bottom, mPaint);
? ? ? ? }
? ? }
}
3、onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)方法:
這個(gè)方法是在item的onDraw()方法之后進(jìn)行回調(diào),也就繪制在了最上層
由于?onDrawOver 是繪制在最上層的,所以它的繪制位置并不受限制,
只要手指在RecyclerView上進(jìn)行滑動(dòng),onDrawOver()方法就會(huì)被回調(diào)。但onDrawOver()每回調(diào)一次,會(huì)將上次的繪制清除,只有最后一次的繪制會(huì)被保留。也就是說繪制的蒙層在屏幕只會(huì)有一個(gè)
所以利用 onDrawOver 可以做很多事情,例如為 RecyclerView 整體頂部繪制一個(gè)蒙層,或者為特定的 item view 繪制蒙層。這里就不單獨(dú)進(jìn)行測(cè)試了。
繪制九宮格布局樣式分割線:
對(duì)于分割線,前面的DividerItemDecoration就不適用了,主要是因?yàn)樗诶L制的時(shí)候,比如水平線,因?yàn)槊總€(gè)Item一行,這樣是沒問題的。而GridLayoutManager時(shí),一行有多個(gè)childItem,這樣就多次繪制了,并且GridLayoutManager時(shí),Item如果為最后一列(則右邊無間隔線)或者為最后一行(底部無分割線)。
主要在getItemOffsets方法中,去判斷如果是最后一行,則不需要繪制底部;如果是最后一列,則不需要繪制右邊,整個(gè)判斷也考慮到了StaggeredGridLayoutManager的橫向和縱向,所以稍稍有些復(fù)雜
1、getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)方法:
@Override
? @Deprecated
? public void getItemOffsets(Rect outRect, int itemPosition,
? ? ? ? ? ? ? ? ? ? ? ? RecyclerView parent) {
? ? ? // 四個(gè)方向的偏移值
? ? ? int right = mDivider.getIntrinsicWidth();
? ? ? int bottom = mDivider.getIntrinsicHeight();
? ? ? if(isLastColum(itemPosition,parent)){//是否是最后一列
//? ? ? outRect.set(0, 0, 0, bottom);
? ? ? ? right = 0;
? ? ? }
? ? ? if(isLastRow(itemPosition,parent)){//是最后一行
//? ? ? outRect.set(0, 0, right, 0);
? ? ? ? bottom = 0;
? ? ? }
? ? ? outRect.set(0, 0, right, bottom);
? }
/**
* 是否是最后一行
* @param itemPosition
* @param parent
* @return
*/
private boolean isLastRow(int itemPosition, RecyclerView parent) {
? int spanCount =? getSpanCount(parent);
? LayoutManager layoutManager = parent.getLayoutManager();
? //有多少列
? if(layoutManager instanceof GridLayoutManager){
? ? ? int childCount = parent.getAdapter().getItemCount();
? ? ? int lastRowCount = childCount%spanCount;
? ? ? //最后一行的數(shù)量小于spanCount
? ? ? if(lastRowCount==0||lastRowCount
? ? ? ? return true;
? ? ? }
? }
? return false;
}
/**
* 判斷是否是最后一列
* @param itemPosition
* @param parent
* @return
*/
private boolean isLastColum(int itemPosition, RecyclerView parent) {
? LayoutManager layoutManager = parent.getLayoutManager();
? //有多少列
? if(layoutManager instanceof GridLayoutManager){
? ? ? int spanCount = getSpanCount(parent);
? ? ? if((itemPosition+1)%spanCount==0){
? ? ? ? return true;
? ? ? }
? }
? return false;
}
2、onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)方法:
@Override
public void onDraw(Canvas c, RecyclerView parent, State state) {
? drawVertical(c,parent);
? drawHorizontal(c,parent);
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
? // 繪制水平間隔線
? int childCount = parent.getChildCount();
? for (int i = 0; i < childCount; i++) {
? ? ? View child = parent.getChildAt(i);
? ? ? RecyclerView.LayoutParams params = (LayoutParams) child.getLayoutParams();
? ? ? int left = child.getLeft() - params.leftMargin;
? ? ? int right = child.getRight()+ params.rightMargin;
? ? ? int top = child.getBottom() + params.bottomMargin;
? ? ? int bottom = top + mDivider.getIntrinsicHeight();
? ? ? mDivider.setBounds(left, top, right, bottom);
? ? ? mDivider.draw(c);
? }
}
private void drawVertical(Canvas c, RecyclerView parent) {
? //繪制垂直間隔線(垂直的矩形)
? int childCount = parent.getChildCount();
? for (int i = 0; i < childCount; i++) {
? ? ? View child = parent.getChildAt(i);
? ? ? RecyclerView.LayoutParams params = (LayoutParams) child.getLayoutParams();
? ? ? int left = child.getRight() + params.rightMargin;
? ? ? int right = left + mDivider.getIntrinsicWidth();
? ? ? int top = child.getTop() - params.topMargin;
? ? ? int bottom = child.getBottom() + params.bottomMargin;
? ? ? mDivider.setBounds(left, top, right, bottom);
? ? ? mDivider.draw(c);
? }
}
源碼demo鏈接:
https://github.com/heiyl/recyleview
csdn地址:https://blog.csdn.net/hylxnq/article/details/80292537