码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView的万能分割线

时间:2016-09-17 19:10:59      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

效果图:

技术分享

使用方法:

添加默认分割线:高度为2px,颜色为灰色

1 mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));

添加自定义分割线:可自定义分割线drawable

1 mRecyclerView.addItemDecoration(new RecycleViewDivider(
2     mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));

添加自定义分割线:可自定义分割线高度和颜色

1 mRecyclerView.addItemDecoration(new RecycleViewDivider(
2     mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color)));

万能分割线登场:

  1 public class RecycleViewDivider extends RecyclerView.ItemDecoration {
  2 
  3     private Paint mPaint;
  4     private Drawable mDivider;
  5     private int mDividerHeight = 2;//分割线高度,默认为1px
  6     private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
  7     private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
  8 
  9     /**
 10      * 默认分割线:高度为2px,颜色为灰色
 11      *
 12      * @param context
 13      * @param orientation 列表方向
 14      */
 15     public RecycleViewDivider(Context context, int orientation) {
 16         if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
 17             throw new IllegalArgumentException("请输入正确的参数!");
 18         }
 19         mOrientation = orientation;
 20 
 21         final TypedArray a = context.obtainStyledAttributes(ATTRS);
 22         mDivider = a.getDrawable(0);
 23         a.recycle();
 24     }
 25 
 26     /**
 27      * 自定义分割线
 28      *
 29      * @param context
 30      * @param orientation 列表方向
 31      * @param drawableId  分割线图片
 32      */
 33     public RecycleViewDivider(Context context, int orientation, int drawableId) {
 34         this(context, orientation);
 35         mDivider = ContextCompat.getDrawable(context, drawableId);
 36         mDividerHeight = mDivider.getIntrinsicHeight();
 37     }
 38 
 39     /**
 40      * 自定义分割线
 41      *
 42      * @param context
 43      * @param orientation   列表方向
 44      * @param dividerHeight 分割线高度
 45      * @param dividerColor  分割线颜色
 46      */
 47     public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {
 48         this(context, orientation);
 49         mDividerHeight = dividerHeight;
 50         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 51         mPaint.setColor(dividerColor);
 52         mPaint.setStyle(Paint.Style.FILL);
 53     }
 54 
 55 
 56     //获取分割线尺寸
 57     @Override
 58     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
 59         super.getItemOffsets(outRect, view, parent, state);
 60         outRect.set(0, 0, 0, mDividerHeight);
 61     }
 62 
 63     //绘制分割线
 64     @Override
 65     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
 66         super.onDraw(c, parent, state);
 67         if (mOrientation == LinearLayoutManager.VERTICAL) {
 68             drawVertical(c, parent);
 69         } else {
 70             drawHorizontal(c, parent);
 71         }
 72     }
 73 
 74     //绘制横向 item 分割线
 75     private void drawHorizontal(Canvas canvas, RecyclerView parent) {
 76         final int left = parent.getPaddingLeft();
 77         final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
 78         final int childSize = parent.getChildCount();
 79         for (int i = 0; i < childSize; i++) {
 80             final View child = parent.getChildAt(i);
 81             RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
 82             final int top = child.getBottom() + layoutParams.bottomMargin;
 83             final int bottom = top + mDividerHeight;
 84             if (mDivider != null) {
 85                 mDivider.setBounds(left, top, right, bottom);
 86                 mDivider.draw(canvas);
 87             }
 88             if (mPaint != null) {
 89                 canvas.drawRect(left, top, right, bottom, mPaint);
 90             }
 91         }
 92     }
 93 
 94     //绘制纵向 item 分割线
 95     private void drawVertical(Canvas canvas, RecyclerView parent) {
 96         final int top = parent.getPaddingTop();
 97         final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
 98         final int childSize = parent.getChildCount();
 99         for (int i = 0; i < childSize; i++) {
100             final View child = parent.getChildAt(i);
101             RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
102             final int left = child.getRight() + layoutParams.rightMargin;
103             final int right = left + mDividerHeight;
104             if (mDivider != null) {
105                 mDivider.setBounds(left, top, right, bottom);
106                 mDivider.draw(canvas);
107             }
108             if (mPaint != null) {
109                 canvas.drawRect(left, top, right, bottom, mPaint);
110             }
111         }
112     }
113 }

 

RecyclerView的万能分割线

标签:

原文地址:http://www.cnblogs.com/huolongluo/p/5879292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!