RecyclerView分割线
来源:互联网 发布:sql查询学生成绩 编辑:程序博客网 时间:2024/06/05 21:52
效果图:
使用方法:
添加默认分割线:高度为2px,颜色为灰色
1 mRecyclerView.addItemDecoration(new RecyclerViewDivider(mContext, LinearLayoutManager.VERTICAL));
添加自定义分割线:可自定义分割线drawable
1 mRecyclerView.addItemDecoration(new RecyclerViewDivider(2 mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));
添加自定义分割线:可自定义分割线高度和颜色
1 mRecyclerView.addItemDecoration(new RecyclerViewDivider(2 mContext, LinearLayoutManager.VERTICAL, 10, ContextCompat.getColor(mContext, R.color.divide_gray_color)));
万能分割线登场:
1 package utils; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.graphics.Rect; 8 import android.graphics.drawable.Drawable; 9 import android.support.v4.content.ContextCompat; 10 import android.support.v7.widget.LinearLayoutManager; 11 import android.support.v7.widget.RecyclerView; 12 import android.view.View; 13 14 /** 15 * Created by ${火龙裸先生} on 2016/10/29. 16 * 邮箱:791335000@qq.com 17 */ 18 public class RecyclerViewDivider extends RecyclerView.ItemDecoration { 19 private Paint mPaint; 20 private Drawable mDivider; 21 private int mDividerHeight = 2;//分割线高度,默认为1px 22 private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL 23 private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 24 25 /** 26 * 默认分割线:高度为2px,颜色为灰色 27 * 28 * @param context 29 * @param orientation 列表方向 30 */ 31 public RecyclerViewDivider(Context context, int orientation) { 32 if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) { 33 throw new IllegalArgumentException("请输入正确的参数!"); 34 } 35 mOrientation = orientation; 36 37 final TypedArray a = context.obtainStyledAttributes(ATTRS); 38 mDivider = a.getDrawable(0); 39 a.recycle(); 40 } 41 42 /** 43 * 自定义分割线 44 * 45 * @param context 46 * @param orientation 列表方向 47 * @param drawableId 分割线图片 48 */ 49 public RecyclerViewDivider(Context context, int orientation, int drawableId) { 50 this(context, orientation); 51 mDivider = ContextCompat.getDrawable(context, drawableId); 52 mDividerHeight = mDivider.getIntrinsicHeight(); 53 } 54 55 /** 56 * 自定义分割线 57 * 58 * @param context 59 * @param orientation 列表方向 60 * @param dividerHeight 分割线高度 61 * @param dividerColor 分割线颜色 62 */ 63 public RecyclerViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) { 64 this(context, orientation); 65 mDividerHeight = dividerHeight; 66 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 67 mPaint.setColor(dividerColor); 68 mPaint.setStyle(Paint.Style.FILL); 69 } 70 71 72 //获取分割线尺寸 73 @Override 74 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 75 super.getItemOffsets(outRect, view, parent, state); 76 outRect.set(0, 0, 0, mDividerHeight); 77 } 78 79 //绘制分割线 80 @Override 81 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 82 super.onDraw(c, parent, state); 83 if (mOrientation == LinearLayoutManager.VERTICAL) { 84 drawVertical(c, parent); 85 } else { 86 drawHorizontal(c, parent); 87 } 88 } 89 90 //绘制横向 item 分割线 91 private void drawHorizontal(Canvas canvas, RecyclerView parent) { 92 final int left = parent.getPaddingLeft(); 93 final int right = parent.getMeasuredWidth() - parent.getPaddingRight(); 94 final int childSize = parent.getChildCount(); 95 for (int i = 0; i < childSize; i++) { 96 final View child = parent.getChildAt(i); 97 RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); 98 final int top = child.getBottom() + layoutParams.bottomMargin; 99 final int bottom = top + mDividerHeight;100 if (mDivider != null) {101 mDivider.setBounds(left, top, right, bottom);102 mDivider.draw(canvas);103 }104 if (mPaint != null) {105 canvas.drawRect(left, top, right, bottom, mPaint);106 }107 }108 }109 110 //绘制纵向 item 分割线111 private void drawVertical(Canvas canvas, RecyclerView parent) {112 final int top = parent.getPaddingTop();113 final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();114 final int childSize = parent.getChildCount();115 for (int i = 0; i < childSize; i++) {116 final View child = parent.getChildAt(i);117 RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();118 final int left = child.getRight() + layoutParams.rightMargin;119 final int right = left + mDividerHeight;120 if (mDivider != null) {121 mDivider.setBounds(left, top, right, bottom);122 mDivider.draw(canvas);123 }124 if (mPaint != null) {125 canvas.drawRect(left, top, right, bottom, mPaint);126 }127 }128 }129 130 }
0 0
- RecyclerView分割线
- RecyclerView分割线详解
- RecyclerView分割线
- RecyclerView虚线分割线
- Android Recyclerview分割线
- RecyclerView(实现分割线)
- RecyclerView万能分割线
- recyclerview分割线 -总结
- RecyclerView分割线
- RecyclerView分割线
- RecyclerView自定义分割线
- RecyclerView 分割线
- RecyclerView加载分割线
- RecyclerView分割线
- RecyclerView分割线
- RecyclerView的分割线
- RecyclerView添加分割线
- RecyclerView分割线
- 多线程之lock和synchronized的区别
- 关于正则表达式自己的
- freemarker.template.TemplateException: Expected string. column.nullable evaluated instead to freemar
- 管道命令(pipe)
- QPixmap显示图片
- RecyclerView分割线
- mysql计算字符串在长字符串中出现次数
- webView打开本地文件和使用第三方网页,以及使用自己的webViewClient
- java验证码
- HTML`CSS_网站页面不同浏览器兼容性问题解决
- SpringBoot 通过属性文件外置配置
- 5.11
- ios 延迟的几种方法
- Minifilter文件系统过滤框架