RecyclerView的万能分割线

Posted 火龙裸先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView的万能分割线相关的知识,希望对你有一定的参考价值。

效果图:

技术分享

使用方法:

添加默认分割线:高度为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的万能分割线的主要内容,如果未能解决你的问题,请参考以下文章

偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder

1.Android recycleView万能分隔线 GridLayoutManager布局item左右间距均等(最易懂)

万能RecyclerView的数据适配器BaseRecyclerViewAdapterHelper

Android:RecyclerView 不显示片段中的列表项

如何从 Firebase 获取数据到 Recyclerview 中的片段?

RecyclerView 内容未使用片段父级的全宽