RecyclerView 添加自定義分割線

默認的淺灰色的分割線在某些時候並不能知足咱們的要求,這時就須要自定義分割線了。android

咱們能夠經過兩種方式來實現:調用 DividerItemDecoration.setDrawable 方法或者繼承實現 RecyclerView.ItemDecoration 類來實現。canvas

1、調用 DividerItemDecoration.setDrawable 方法

實現分割線只須要調用 setDrawable(@NonNull Drawable drawable)方法,而後傳入一個Drawable函數對象就能夠了。ide

如今能夠用shape來編寫一個分割線樣式:函數

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:centerColor="#ff00ff00" //綠色
        android:endColor="#ff0000ff"    //藍色
        android:startColor="#ffff0000"  //紅色
        android:type="linear" />
    <size android:height="3dp" />

</shape>

添加分割線的代碼改成以下:this

//添加自定義分割線
DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.custom_divider));
recyclerView.addItemDecoration(divider);

運行起來以後,就能夠看到一條多彩的分割線了:spa

2、繼承實現 RecyclerView.ItemDecoration 類

這塊就很少贅述了,直接貼代碼:3d

public class RecyclerViewDivider extends RecyclerView.ItemDecoration{
    private Paint mPaint;
    //分割線
    private Drawable mDivider;
    //分割線高度,默認是2px
    private int mDividerHeight = 2;
    //列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
    private int mOrientation;
    
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    /**
     *
     * 默認分割線:高度爲2px,顏色爲灰色
     * 獲取屬性值,
     *
     * @param context
     * @param orientation  列表方向
     */

    public RecyclerViewDivider(Context context, int orientation){
        if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
            throw new IllegalArgumentException("請輸入正確的參數!");
        }
        mOrientation = orientation;
        final TypedArray array = context.obtainStyledAttributes(ATTRS);
        mDivider = array.getDrawable(0);
        array.recycle();
        mDividerHeight = mDivider.getIntrinsicHeight();
    }

    /**
     * 自定義分割線
     *
     * @param context
     * @param orientation 列表方向
     * @param drawableId  分割線圖片
     */
    public RecyclerViewDivider(Context context, int orientation, int drawableId) {
        if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
            throw new IllegalArgumentException("請輸入正確的參數!");
        }
        mOrientation = orientation;
        
        mDivider = ContextCompat.getDrawable(context, drawableId);
        mDividerHeight = mDivider.getIntrinsicHeight();
    }
    

    /**
     * 自定義分割線
     *
     * @param context
     * @param orientation   列表方向
     * @param dividerHeight 分割線高度
     * @param dividerColor  分割線顏色
     */
    public RecyclerViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {

        if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
            throw new IllegalArgumentException("請輸入正確的參數!");
        }
        mOrientation = orientation;
        
        mDividerHeight = dividerHeight;
        
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(dividerColor);
        mPaint.setStyle(Paint.Style.FILL);
    
    }


    //獲取分割線尺寸
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }

        outRect.set(0, 0, 0, mDividerHeight);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if(mOrientation==LinearLayoutManager.VERTICAL){
            drawVerticalLine(c,parent);
        }else{
            drawHorizontalLine(c,parent);
        }
    }

    //爲橫方向item, 畫分割線
    private void drawHorizontalLine(Canvas canvas, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + layoutParams.rightMargin;
            final 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);
            }
        }
    }

    //爲豎方向item, 畫分割線
    private void drawVerticalLine(Canvas canvas, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + layoutParams.bottomMargin;
            final 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);
            }
        }
    }
}
相關文章
相關標籤/搜索