RecyclerView的分割线:ItemDecoration

使用ListView时,设置分割线很容易,但是用RecyclerView就会麻烦一点。一般都需要重写ItemDecoration,并给RecyclerView设置ItemDecoration。下面来介绍一下ItemDecoration中几个比较重要的方法。

  • getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)

    Retrieve any offsets for the given item. Each field of outRect 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.

设置分割线所占的大小,给分割线预留位置。

  • onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)

    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.

在RecyclerView的item被draw之前,为RecyclerView设置装饰(一般都是指分割线),所以此种情况下当getItemOffsets设置过小,分割线会被item遮住。

  • onDrawOver(Canvas c, RecyclerView parent, State state)

    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.

在RecyclerView的item被draw之后,为RecyclerView设置装饰(一般都是指分割线),所以此种情况下当getItemOffsets设置过小,分割线会遮住item的内容。


下面贴上一个自定义分割线的代码,

RecyclerView mRecyclerView=findViewById();
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getResources().getColor(R.color.s5)
                    , 1, LinearLayoutManager.VERTICAL);
    public class DividerItemDecoration extends RecyclerView.ItemDecoration {

        private Paint mPaint;
        private int mColor;
        private int mDividerSize;
        private int mOrientation;

        //分割线与两边的距离
        private float mStartMargin;
        private float mEndMargin;

        public DividerItemDecoration(int color, int dividerSize, int orientation) {
            this(color, dividerSize, orientation, 0, 0);
        }

        public DividerItemDecoration(int color, int dividerSize, int orientation, float startMargin, float endMargin) {
            mColor = color;
            mDividerSize = dividerSize;
            mOrientation = orientation;
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(mColor);
            mPaint.setStyle(Paint.Style.FILL);
            mStartMargin = startMargin;
            mEndMargin = endMargin;
        }

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

      //水平的list,画竖直的分割线
        private void drawHorizontalDivider(Canvas c, RecyclerView parent) {
            int top = 0;
            int bottom = parent.getMeasuredHeight();
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                int pos = parent.getChildAdapterPosition(child);
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                int left = child.getLeft() + layoutParams.leftMargin;
                int right = top + mDividerSize;
                c.drawRect(left, top + mStartMargin
                        , right, bottom - mEndMargin, mPaint);
            }
        }

      //竖直的list,画水平的分割线
        private void drawVerticalDivider(Canvas c, RecyclerView parent) {
            final int left = 0;
            final int right = parent.getMeasuredWidth();
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int top = child.getTop() + layoutParams.topMargin;
                final int bottom = top + mDividerSize;
                c.drawRect(left + mStartMargin, top
                        , right - mEndMargin, bottom, mPaint);
            }
        }

      //设置分割线展位大小
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int pos = parent.getChildAdapterPosition(view);
            if (mOrientation == LinearLayoutManager.VERTICAL) {
                outRect.set(0, 0, 0, mDividerSize);
            } else {
                outRect.set(0, 0, mDividerSize, 0);
            }
        }

    }

你可能感兴趣的:(Android学习)