練習RecyclerView添加分割線

RecyclerView需要手動添加分割線,之前每次都是把代碼復制粘貼上去了。剛剛練習了一下如何添加豎線分割線和橫向間隔線以及雙向添加分割線。現在記錄一下,但是繪制圖片部分需要修改,具體該怎么弄我要找一下資料。先放上普通單色線。




import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

/**
 * <pre>
 *  名稱:RecyclerViewItemDecoration
 *  作用:RecyclerView分割線
 *  描述:
 *  作者:pczhu
 *  創建時間: 2017/6/14 上午11:49
 *  版本:V1.0
 *  修改歷史:
 *  </pre>
 */

public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {

    private Context mContext;
    /**
     * <pre>
     *      方向參數,默認縱向
     *      0x10000000:Vertical縱向
     *      0x20000000:Horizontal橫向
     * </pre>
     */
    private int mOrientation = 0x10000000;
    /**
     * <pre>
     *      橫向
     * </pre>
     */
    public static final int Horizontal = 0x10000000;
    /**
     * <pre>
     *      縱向
     * </pre>
     */
    public static final int Vertical = 0x20000000;
    private Paint mPaint;
    /**
     * <pre>
     *      顏色參數:分割線顏色
     *      默認顏色:#eeeeee
     * </pre>
     */
    private int mDividerColor =  Color.parseColor("#000000");
    /**
     * <pre>
     *      分割線高度
     *          默認2sp
     * </pre>
     */
    private int mDividerHeight = 2;
    /**
     * <pre>
     *     分割線寬度
     *          默認2sp
     * </pre>
     */
    private int mDividerWidth = 2;
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;

    private void setDividerColor(int mDividerColor) {
        this.mDividerColor = mDividerColor;
    }

    private void setDividerHeight(int mDividerHeight) {
        this.mDividerHeight = dip2px(mDividerHeight);
    }

    private void setDividerWidth(int mDividerWidth) {
        this.mDividerWidth = dip2px(mDividerWidth);
    }

    private void setDivider(Drawable mDivider) {
        this.mDivider = mDivider;
    }

    public void setOrientation(int mOrientation) {
        this.mOrientation = mOrientation;
    }

    /**
     * 默認分割線:高度為2px,顏色為灰色
     *
     * @param context
     * @param orientation 列表方向
     */
    public RecyclerViewItemDecoration(Context context, int orientation) {
        this.mContext = context;
        if ((orientation & LinearLayoutManager.VERTICAL) == 1
                && (orientation & LinearLayoutManager.HORIZONTAL) == 1) {
            throw new IllegalArgumentException("請輸入正確的參數!");
        }
        mOrientation = orientation;

        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        if(mDivider==null){
            initPaint();
        }
    }


    /**
     * 初始化畫筆
     */
    private void initPaint() {
        mPaint = new Paint(ANTI_ALIAS_FLAG);
        mPaint.setColor(mDividerColor);
    }
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if(getLayoutManagerType(parent) != 2){

            if(mOrientation == (Horizontal|Vertical)){
                drawDividerBothLine(c,parent);
            }else if(mOrientation == Vertical){
                drawDividerVertical(c, parent);
            }else {
                drawDividerHorizontal(c,parent);
            }
        }

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int spanCount = getSpanCount(parent);
        //特殊的item間距
        if(getLayoutManagerType(parent) == 2){//流 折半 每個偏移一半的值 相鄰就會偏移一個
            outRect.left = mDividerWidth/2;
            outRect.right = mDividerWidth/2;
            outRect.bottom = mDividerHeight/2;
            outRect.top = mDividerHeight/2;

        }else {//其他分割線
            if(mOrientation == (Horizontal|Vertical)){
                if((parent.getChildAdapterPosition(view)+1)%spanCount == 0){
                    outRect.set(0,0,0,mDividerHeight);
                }else {
                    outRect.set(0,0,mDividerWidth,mDividerHeight);
                }
            }else if(mOrientation == Vertical){
                outRect.set(0,0,0,mDividerHeight);
            }else if(mOrientation == Horizontal){
                outRect.set(0,0,mDividerHeight,0);
            }

        }
    }

    /**
     * 劃豎排列表的分割線
     * @param c
     * @param parent
     */
    private void drawDividerVertical(Canvas c,RecyclerView parent){
        int leftParent = parent.getPaddingLeft();
        int rightParent = parent.getWidth()-parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for(int i = 0;i < childCount ; i++){
            View mChildView = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) mChildView.getLayoutParams();

            int left = leftParent+layoutParams.leftMargin;
            int right = rightParent-layoutParams.rightMargin;
            int top = mChildView.getBottom()+layoutParams.bottomMargin;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, top+mDividerHeight);
                mDivider.draw(c);
            }
            if (mPaint != null) {
//                Log.i("定點", left + ":" + top + ":" + right + ":" + (top + mDividerHeight));
                Rect rect = new Rect(left, top, right, (top + mDividerHeight));
                c.drawRect(rect, mPaint);
            }
        }
    }

    /**
     * 劃橫排列表間隔線
     * @param canvas
     * @param parent
     */
    private void drawDividerHorizontal(Canvas canvas, RecyclerView parent) {
        int top = parent.getPaddingTop();
        int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            View mChildView = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) mChildView.getLayoutParams();
            int left = mChildView.getRight() + layoutParams.rightMargin;
            int right = left + mDividerHeight;

            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);

            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    /**
     * 同時畫橫向間隔線 豎向分割線
     * @param canvas
     * @param parent
     */
    private void drawDividerBothLine(Canvas canvas, RecyclerView parent) {
        int spanCount = getSpanCount(parent);
        int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            View mChildView = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) mChildView.getLayoutParams();

            if((i+1) % spanCount == 0){//劃豎向行分界線

                int left = parent.getPaddingLeft()+layoutParams.leftMargin;
                int right = parent.getMeasuredWidth()-parent.getPaddingRight()-layoutParams.rightMargin;
                int top = mChildView.getBottom()+layoutParams.bottomMargin;
                int bottom = top+mDividerHeight;
                if (mDivider != null) {
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }else{//劃橫向item間線

                int left = mChildView.getRight() + layoutParams.rightMargin;
                int right = left + mDividerWidth;
                int top = mChildView.getTop()-layoutParams.topMargin;
                int bottom = mChildView.getTop()+mChildView.getMeasuredHeight()+layoutParams.bottomMargin;
                if (mDivider != null) {
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }
    /**
     * 獲取列數
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        int spanCount = -1;
        switch (getLayoutManagerType(parent)){
            case 1:
                spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
                break;
            case 2:
                spanCount = ((StaggeredGridLayoutManager) layoutManager)
                        .getSpanCount();
                break;

        }
        return spanCount;
    }

    /**
     * 返回manager類型
     * @param parent
     * @return
     */
    private int getLayoutManagerType(RecyclerView parent){
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            return 1;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            return 2;
        } else if (layoutManager instanceof LinearLayoutManager){
            return 3;
        }else{
            return -1;
        }
    }
    public static class Builder{
        private Context mContext;
        RecyclerViewItemDecoration recyclerViewItemDecoration;
        /**
         * <pre>
         *      方向參數,默認縱向
         *      0x10000000:Vertical縱向
         *      0x20000000:Horizontal橫向
         * </pre>
         */
        private int mOrientation = 0x10000000;
        /**
         * <pre>
         *      顏色參數:分割線顏色
         *      默認顏色:#eeeeee
         * </pre>
         */
        private int mDividerColor =  Color.parseColor("#000000");
        /**
         * <pre>
         *      分割線高度
         *          默認2sp
         * </pre>
         */
        private int mDividerHeight = 2;
        /**
         * <pre>
         *     分割線寬度
         *          默認2sp
         * </pre>
         */
        private int mDividerWidth = 2;
        /**
         * <pre>
         *     作為分割線的圖片資源
         * </pre>
         */
        private Drawable mDivider;

        public Builder(Context mContext) {
            this.mContext = mContext;
            recyclerViewItemDecoration = new RecyclerViewItemDecoration(mContext,mOrientation);
        }

        /**
         * 設置方向
         * @param mOrientation
         * @return
         */
        public Builder setOrientation(int mOrientation) {
            this.mOrientation = mOrientation;
            recyclerViewItemDecoration.setOrientation(mOrientation);
            return this;
        }

        /**
         * 設置顏色
         * @param mDividerColor
         * @return
         */
        public Builder setDividerColor(int mDividerColor) {
            this.mDividerColor = mDividerColor;
            recyclerViewItemDecoration.setDividerColor(mDividerColor);
            return this;
        }

        /**
         * 設置縱向分割線高度
         * @param mDividerHeight dp單位
         * @return
         */
        public Builder setDividerHeight(int mDividerHeight) {
            this.mDividerHeight = mDividerHeight;
            recyclerViewItemDecoration.setDividerHeight(mDividerHeight);
            return this;
        }

        /**
         * 設置分割線寬度
         * @param mDividerWidth dp單位
         * @return
         */
        public Builder setDividerWidth(int mDividerWidth) {
            this.mDividerWidth = mDividerWidth;
            recyclerViewItemDecoration.setDividerWidth(mDividerWidth);
            return this;
        }

        /**
         * 設置分割線圖片
         * @param mDivider
         * @return
         */
        public Builder setDivider(Drawable mDivider) {
            this.mDivider = mDivider;
            recyclerViewItemDecoration.setDivider(mDivider);
            return this;
        }
        public RecyclerViewItemDecoration build(){
            return recyclerViewItemDecoration;

        }
    }


    public int dip2px(float dpValue) {
        if(mContext == null){
            return 2;
        }
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根據手機的分辨率從 px(像素) 的單位 轉成為 dp
     */
    public int px2dip(float pxValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}


        mRecyclerView.addItemDecoration(new RecyclerViewItemDecoration.Builder(this)
                .setOrientation(RecyclerViewItemDecoration.Vertical|RecyclerViewItemDecoration.Horizontal)
                .setDividerHeight(4)
                .setDividerWidth(4)
//                .setDivider(getResources().getDrawable(R.mipmap.abc))
                .setDividerColor(Color.parseColor("#eebbdd"))
                .build());
Markdown

Markdown
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容