public class DividerLinearItemDecoration extends RecyclerView.ItemDecoration {
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private int dividerSpace;//分割条宽度
private ColorDrawable mDivider;//分割条颜色
private int mOrientation;
public DividerLinearItemDecoration(Context context, int orientation) {
dividerSpace = dip2px(context, 1);
mDivider = new ColorDrawable(Color.TRANSPARENT);
setOrientation(orientation);
}
public void setDividerSpace(int dividerSpace) {
this.dividerSpace = dividerSpace;
}
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);
}
}
/**
* 创建的范围:
* (1)垂直的时候
* 左边=RecyclerView的左边-paddingLeft
* 右边=RecyclerView的右边-paddingRight
* 为啥这么做呢?
* 因为mDivider是绘制的item view的最低层,在垂直方向,item view的间隔是上下的,
* mDivider的范围就超出前面显示的内容,即左右突出了
* 顶边=
**/
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 +
Math.round(ViewCompat.getTranslationY(child));
final int bottom = top + dividerSpace;
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 +
Math.round(ViewCompat.getTranslationX(child));
final int right = left + dividerSpace;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 这个方法比onDraw()先执行
* 这里的作用是设置Rect的范围,
* RecyclerView会调用这个方法来设置每个item view的padding值
* Rect对应item view:
* left=paddingLeft
* right=paddingLeft
* top=paddingTop
* bottom=paddingBottom
**/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
//垂直方向
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, dividerSpace);//垂直滚动则设置paddingBottom的值
return;
}
//水平方向
if (position == parent.getChildCount()) {
outRect.set(0, 0, 0, 0);//最后一个item,尾部不需要间隔
} else {
outRect.set(0, 0, dividerSpace, 0);//水平滚动则设置paddingRight的值
}
}
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}}
RecyclerView.ItemDecoration
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- RecyclerView.ItemDecoration不仅仅只是用来画线实现分割线这么简单; 你可以绘制every...
- 1.背景 RecyclerView 是谷歌 V7 包下新增的控件,用来替代 ListView 和 GridView...
- 前言 RecyclerView在 Android开发中非常常用,如果能结合ItemDecoration类使用,那么...
- RecyclerView 作为取代ListView的控件已经出来很长一段时间了,介绍RecyclerView的文章...