RecyclerView自定义ItemDecoration

来源:互联网 发布:淘宝卖家学院光云科技 编辑:程序博客网 时间:2024/05/01 11:40

     从ItemDecoration的源码:

public static abstract class ItemDecoration {    /**     * Draw any appropriate decorations into the Canvas supplied to the RecyclerView.     * Any content drawn by this method will be drawn before the item views are drawn,     * and will thus appear underneath the views.     *     * @param c Canvas to draw into     * @param parent RecyclerView this ItemDecoration is drawing into     * @param state The current state of RecyclerView     */    public void onDraw(Canvas c, RecyclerView parent, State state) {        onDraw(c, parent);    }    /**     * @deprecated     * Override {@link #onDraw(Canvas, RecyclerView, RecyclerView.State)}     */    @Deprecated    public void onDraw(Canvas c, RecyclerView parent) {    }    /**     * Draw any appropriate decorations into the Canvas supplied to the RecyclerView.     * Any content drawn by this method will be drawn after the item views are drawn     * and will thus appear over the views.     *     * @param c Canvas to draw into     * @param parent RecyclerView this ItemDecoration is drawing into     * @param state The current state of RecyclerView.     */    public void onDrawOver(Canvas c, RecyclerView parent, State state) {        onDrawOver(c, parent);    }    /**     * @deprecated     * Override {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)}     */    @Deprecated    public void onDrawOver(Canvas c, RecyclerView parent) {    }    /**     * @deprecated     * Use {@link #getItemOffsets(Rect, View, RecyclerView, State)}     */    @Deprecated    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {        outRect.set(0, 0, 0, 0);    }    /**     * Retrieve any offsets for the given item. Each field of <code>outRect</code> specifies     * the number of pixels that the item view should be inset by, similar to padding or margin.     * The default implementation sets the bounds of outRect to 0 and returns.     *     * <p>     * If this ItemDecoration does not affect the positioning of item views, it should set     * all four fields of <code>outRect</code> (left, top, right, bottom) to zero     * before returning.     *     * <p>     * If you need to access Adapter for additional data, you can call     * {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the     * View.     *     * @param outRect Rect to receive the output.     * @param view    The child view to decorate     * @param parent  RecyclerView this ItemDecoration is decorating     * @param state   The current state of RecyclerView.     */    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {        getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),                parent);    }}
可以知道:

      而我们定制Item 分割线真正要用到的方法其实只有两个,
     1、画分割线     public void onDraw(Canvas c, RecyclerView parent, State state) {

                    }
     2、设置分割线的size   public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {

                          }
 


public class DividerItemDecoration extends RecyclerView.ItemDecoration{    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;     /**     * RecyclerView的布局方向,默认先赋值     * 为纵向布局     * RecyclerView 布局可横向,也可纵向     * 横向和纵向对应的分割想画法不一样     * */    private int mOrientation;    /**     * item之间分割线的size,默认为1     */    private int mItemSize = 1 ;    /**     * 绘制item分割线的画笔,和设置其属性     * 来绘制个性分割线     */    private Paint mPaint ;    /**     * 构造方法传入布局方向,不可不传     * @param context     * @param orientation     */    public DividerItemDecoration(Context context, int orientation) {        setOrientation(orientation);        mItemSize = (int) TypedValue.applyDimension(mItemSize, TypedValue.COMPLEX_UNIT_DIP,context.getResources().getDisplayMetrics());        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ;        mPaint.setColor(Color.BLUE);         /*设置填充*/        mPaint.setStyle(Paint.Style.FILL);    }    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) {//        Log.v("recyclerview - itemdecoration", "onDraw()");        if (mOrientation == VERTICAL_LIST) {            drawVertical(c, parent);        } else {            drawHorizontal(c, parent);        }    }    //绘制垂直分割线    public void drawVertical(Canvas canvas, 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);            android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child                    .getLayoutParams();            final int top = child.getBottom() + params.bottomMargin;            final int bottom = top + mItemSize;            canvas.drawRect(left,top,right,bottom,mPaint);        }    }    //绘制水平分割线    public void drawHorizontal(Canvas canvas, 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;            final int right = left + mItemSize ;            canvas.drawRect(left,top,right,bottom,mPaint);        }    }    @Override    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {        if (mOrientation == VERTICAL_LIST) {            outRect.set(0, 0, 0, mItemSize);        } else {            outRect.set(0, 0, mItemSize, 0);        }    }}

使用的时候

layoutManager=new LinearLayoutManager(mContext);mRecyclerView.setLayoutManager(layoutManager);//添加分割线mRecyclerView.addItemDecoration(new DividerItemDecoration(this,        DividerItemDecoration.VERTICAL_LIST));// 设置item动画mRecyclerView.setItemAnimator(new DefaultItemAnimator());mRecyclerView.setAdapter(mRecyAdapter);


0 0
原创粉丝点击