RecyclerView 添加分割线

一、使用默认分割线

RecyclerView自带分割线:

设置DividerItemDecoration.VERTICAL时,给item底部添加分割线,
设置DividerItemDecoration.HORIZONTAL时,给item右侧添加分割线

//初始化RecyclerView
private void initRecyclerView() {
    final RecyclerView recycler = findViewById(R.id.recycler);
    final GridLayoutManager manager = new GridLayoutManager(this, 3);
    final RecyclerAdapter adapter = new RecyclerAdapter();
    //第二个参数设置分割线方向,DividerItemDecoration.VERTICAL 表示分割垂直展示的item
    final DividerItemDecoration itemDecoration = 
        new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
    itemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.divider));
    recycler.setLayoutManager(manager);
    recycler.setAdapter(adapter);
    //设置分割线
    recycler.addItemDecoration(itemDecoration);
}

divider.xml



     
     
    
    

HORIZONTAL.png
VERTICAL.png

二、自定义分割线

自定义分割线需要继承ItemDecoration这个类,是 RecyclerView的内部类,主要覆写三个方法:

public abstract static class ItemDecoration {
    //在item绘制之前调用,绘制在item的底层
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    }

    //在item绘制之后调用,绘制在item的上层
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    }

    //通过设置outRect,给item设置四周分割宽度,类似于view的padding、margin
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    }
}

先分析DividerItemDecoration,看官方是怎样实现的,看主要代码:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    //分割线方向
    public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
    public static final int VERTICAL = LinearLayout.VERTICAL;
    
    //系统属性
    private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };
    
    //分割线Drawable资源文件,这里是默认获取系统listDivider资源文件,也可以通过setDrawable()方法,设置自定义分割线
    private Drawable mDivider;
    
    //当前分割线为 HORIZONTAL或VERTICAL
    private int mOrientation;
    //用来存储itemView的margin值
    private final Rect mBounds = new Rect();

   //初始化类,第二个参数为HORIZONTAL或VERTICAL分割
    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        if (mDivider == null) {
            Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this "
                    + "DividerItemDecoration. Please set that attribute all call setDrawable()");
        }
        a.recycle();
        setOrientation(orientation);
    }

   //设置HORIZONTAL或VERTICAL分割
    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL && orientation != VERTICAL) {
            throw new IllegalArgumentException(
                    "Invalid orientation. It should be either HORIZONTAL or VERTICAL");
        }
        mOrientation = orientation;
    }
    //设置自定义drawable分割线资源
    public void setDrawable(@NonNull Drawable drawable) {
        if (drawable == null) {
            throw new IllegalArgumentException("Drawable cannot be null.");
        }
        mDivider = drawable;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //v7包中,只覆写onDraw()方法,表示分割线绘制先绘制,处于itemView的底层
        if (parent.getLayoutManager() == null || mDivider == null) {
            return;
        }
        if (mOrientation == VERTICAL) {
            //画垂直分割线
            drawVertical(c, parent);
        } else {
            //画竖直分割线
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        //1.保存画布
        canvas.save();
        final int left;
        final int right;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        //2.判断recyclerview是否允许ClipToPadding,默认为true,当clipToPadding为true时,表示             布局不能绘制到padding区域。当clipToPadding为false时,表示布局能够被绘制到padding区            域。
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            //只显示选中区域
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }
   
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            //4.获取itemview的Margin值
            parent.getDecoratedBoundsWithMargins(child, mBounds);
            //5.计算分割线底部和顶部
            final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
            final int top = bottom - mDivider.getIntrinsicHeight();
            //6.设置分割线显示区域
            mDivider.setBounds(left, top, right, bottom);
            //7.画分割线
            mDivider.draw(canvas);
        }
        //8.恢复画布
        canvas.restore();
    }

    //类比画垂直分割线
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
       ......
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        //判顿分割线是否为null,为null则设置分割线区域为空
        if (mDivider == null) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        if (mOrientation == VERTICAL) {
            //设置垂直分割线的高度
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            //设置垂直分割线的宽度
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

你可能感兴趣的:(RecyclerView 添加分割线)