package com.yunbao.common.custom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.TypedValue;
import android.view.View;
// +----------------------------------------------------------------------
// | Created by Yunbao
// +----------------------------------------------------------------------
// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: https://gitee.com/yunbaokeji/
// +----------------------------------------------------------------------
// | Date: 2022-02-17
// +----------------------------------------------------------------------
/**
* RecyclerView分割线
*/
public class ItemDecoration extends RecyclerView.ItemDecoration {
private static final String TAG = "ItemDecoration";
private Drawable mDivider;
private int dividerHeight;
private int dividerWidth;
private int dividerColor;
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
/**
* 设置是否显示左右边界线
*/
private boolean drawBorderLeftAndRight = false;
/**
* 设置是否显示上下边界线
*/
private boolean drawBorderTopAndBottom = false;
/**
* 是否只留空白,不画分割线
*/
private boolean onlySetItemOffsetsButNoDraw=false;
/**
* 是否是线性布局
*/
private boolean isLinearLayoutManager=true;
/**
* 布局方向
*/
private int orientation=VERTICAL_LIST;
public ItemDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
this.dividerHeight=mDivider.getIntrinsicHeight();
this.dividerWidth=mDivider.getIntrinsicWidth();
}
/**
* 自定义分割线
*
* @param context
* @param drawableId 分割线图片
*/
public ItemDecoration(Context context, @DrawableRes int drawableId) {
mDivider = ContextCompat.getDrawable(context, drawableId);
this.dividerHeight=mDivider.getIntrinsicHeight();
this.dividerWidth=mDivider.getIntrinsicWidth();
}
/**
* 自定义分割线
* 也可以使用{@link Canvas#drawRect(float, float, float, float, Paint)}或者{@link Canvas#drawText(String, float, float, Paint)}等等
* 结合{@link Paint}去绘制各式各样的分割线
* @param context
* @param color 整型颜色值,非资源id
* @param dividerWidth 单位为dp
* @param dividerHeight 单位为dp
*/
public ItemDecoration(Context context, @ColorInt int color, @Dimension float dividerWidth, @Dimension float dividerHeight) {
mDivider = new ColorDrawable(color);
this.dividerWidth= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dividerWidth,context.getResources().getDisplayMetrics());
this.dividerHeight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dividerHeight,context.getResources().getDisplayMetrics());
}
/**
* 垂直滚动,item宽度充满,高度自适应
* 水平滚动,item高度充满,宽度自适应
* 在itemView绘制完成之前调用,也就是说此方法draw出来的效果将会在itemView的下面
* onDrawOver方法draw出来的效果将叠加在itemView的上面
* @param c
* @param parent
* @param state
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if(onlySetItemOffsetsButNoDraw){
return;
}
if(isLinearLayoutManager){
drawLinearItemDivider(c,parent);
}else{
drawHorizontalLine(c, parent);
drawVerticalLine(c, parent);
}
}
private void drawLinearItemDivider(Canvas c, RecyclerView parent){
int spanCount = getSpanCount(parent);
int allChildCount = parent.getAdapter().getItemCount();
int top=0,bottom=0,left=0,right=0;
for (int i = 0; i < parent.getChildCount(); i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if(orientation==VERTICAL_LIST){//画横线
left = child.getLeft() - params.leftMargin;
right = child.getRight() + params.rightMargin;
if(drawBorderTopAndBottom){
//加上第一条
if(isFirstRaw(parent,params.getViewLayoutPosition(),spanCount)){
top=child.getTop()-params.topMargin-dividerHeight;
bottom = top + dividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}else{
if(isLastRaw(parent,params.getViewLayoutPosition(),spanCount,allChildCount)){
continue;
}
}
top = child.getBottom() + params.bottomMargin;
bottom = top + dividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}else{//画竖线
top=child.getTop()-params.topMargin;
bottom=child.getBottom()+params.bottomMargin;
if(drawBorderLeftAndRight){
//加上第一条
if(isFirstColumn(parent,params.getViewLayoutPosition(),spanCount)){
left=child.getLeft()-params.leftMargin-dividerWidth;
right = left + dividerWidth;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}else{
if(isLastColum(parent,params.getViewLayoutPosition(),spanCount,allChildCount)){
continue;
}
}
left = child.getRight() + params.rightMargin;
right = left + dividerWidth;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
if(orientation==VERTICAL_LIST){
if(drawBorderLeftAndRight){
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
left=parent.getPaddingLeft();
right=left+dividerWidth;
//画左边界
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
left=parent.getWidth()-parent.getPaddingRight()-dividerWidth;
right=left+dividerWidth;
//画右边界
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
}
}else if(orientation==HORIZONTAL_LIST){
if(drawBorderTopAndBottom){
left=parent.getPaddingLeft();
right=parent.getWidth()-parent.getPaddingRight();
top=parent.getPaddingTop();
bottom=top+dividerHeight;