国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

RecyclerView自定義ItemDecoration

2019-11-09 14:47:20
字體:
來源:轉載
供稿:網友

     從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);


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青神县| 崇礼县| 乐平市| 纳雍县| 富川| 修水县| 浮山县| 咸宁市| 福海县| 思茅市| 古蔺县| 汪清县| 纳雍县| 霞浦县| 绥中县| 曲靖市| 汉川市| 新丰县| 封开县| 阳新县| 巴楚县| 桓台县| 北川| 鄂托克前旗| 酒泉市| 天津市| 抚顺市| 苍山县| 宜州市| 永兴县| 宜阳县| 周口市| 保康县| 漳平市| 榆中县| 茌平县| 九龙坡区| 交口县| 抚顺县| 九台市| 孙吴县|