Android RecyclerView.ItemDecorat
2021-08-10 本文已影响0人
zcwfeng
导览:由于平时RecyclerView 经常用分割线,所以每次用都要查询。要么搜api要么搜demo。大概看了下项目用到的整理几个demo。作为备查,大家可以作为字典根据需要更改。没什么技术含量,就是方便一下。
Gride RecyclerView
给gridRecycleView 实现分割线
tip:建议recycleview的长宽模式设为wrapcontent divider的作用主要提供颜色和默认边框值 均可自己设置
public class GridItemDividerDecoration extends RecyclerView.ItemDecoration {
private int mRedundant;
public static final int VERTICAL = 1;
public static final int Horizon = 0;
private Drawable mDivider;
private int mThickness;
private int mOratation;
public GridItemDividerDecoration(Drawable divider, int orantation) {
this.mDivider = divider;
this.mOratation = orantation;
setThickness();
}
public GridItemDividerDecoration(Drawable divider, int orantation, int thick) {//亲测横向纵向都可以的
this.mDivider = divider;
this.mOratation = orantation;
setThickness(thick);
}
private void setThickness() {
if (mOratation == VERTICAL) {
mThickness = mDivider.getIntrinsicHeight();
} else if (mOratation == Horizon) {
mThickness = mDivider.getIntrinsicWidth();
}
}
private void setThickness(int thickness) {//用于自己设置分割线宽度
mThickness = thickness;
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
int spancount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
mRedundant = parent.getChildCount() % spancount;
mRedundant = mRedundant == 0 ? spancount : mRedundant;//最后一排的item个数
final int underlineNum = parent.getChildCount() - mRedundant;//下划线的child的个数
final Drawable divider = mDivider;
final int thickness = mThickness;
for (int i = 0; i < underlineNum; i++) {//给非最后一排的item画边边
View child = parent.getChildAt(i);
if (mOratation == VERTICAL) {
divider.setBounds(child.getLeft(), child.getBottom(), child.getRight(), child.getBottom() + thickness);
}
if (mOratation == Horizon) {
divider.setBounds(child.getRight(), child.getTop(), child.getRight() + thickness, child.getBottom());
}
divider.draw(c);
}
}
}
水平间距 HorizontalItemDecoration
/**
* 定义水平方向的距离
*/
public class HorizontalItemDecoration extends RecyclerView.ItemDecoration {
private int space;//定义2个Item之间的距离
public HorizontalItemDecoration(int space, Context mContext) {
this.space = dip2px(space,mContext);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
int totalCount = parent.getAdapter().getItemCount();
if (position == 0) {//第一个
outRect.left = 0;
outRect.right = space / 2;
} else if (position == totalCount - 1) {//最后一个
outRect.left = space / 2;
outRect.right = 0;
} else {//中间其它的
outRect.left = space / 2;
outRect.right = space / 2;
}
}
public int dip2px(float dpValue,Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
竖直间距
public class MyDecoration extends RecyclerView.ItemDecoration {
private int size = 2;
public MyDecoration(int size) {
this.size = size;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//设置间距
outRect.bottom = size;
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
Paint paint = new Paint();
paint.setColor(BaseApplication.getInstance().getResources().getColor(R.color.c_e6e6e6));
//获取item的数量
int count = parent.getChildCount();
//设置要绘制的矩形的left、right、top、bottom值
int left = parent.getPaddingLeft()+20;
int right = parent.getWidth() - parent.getPaddingRight()-20;
for (int i = 0; i < count; i++){
//获取对应的item
View view = parent.getChildAt(i);
float top = view.getBottom();
float bottom = view.getBottom() + size;
//绘制矩形
c.drawRect(left,top,right,bottom,paint);
}
}
}