RecyclerView.ItemDecoration

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)