[1].RecyclerView可以说是现在安卓视图的一哥了 [2].加包implementation 'com.android.support:design:26.1.0' [3].RecyclerView的布局样式、装饰线
[1].要设置布局管理器,不然无效果 [2].费了这么大的劲,就搞出一个没有线的不能点击的ListView? [3].一开始也觉得,这也不比ListView好哪去啊,别急,且往下看
RecyclerView能对布局的条目进行控制,者也是它强大之处
有一点是每一个条目都绘制全RecyclerView长宽,绘制了很多不必要的线 不过在RecycleViewDivider里我暂时得不到给个view的尺寸...加个TODO吧,先凑合着用以后完善
public class RecycleViewDivider extends RecyclerView.ItemDecoration { private Paint mPaint; private Drawable mDivider; private int mDividerHeight = 2;//分割线高度,默认为1px private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; /** * 默认分割线:高度为2px,颜色为灰色 * * @param context 上下文 * @param orientation 列表方向 */ public RecycleViewDivider(Context context, int orientation) { if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) { throw new IllegalArgumentException("请输入正确的参数!"); } mOrientation = orientation; final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); } /** * 自定义分割线 * * @param context 上下文 * @param orientation 列表方向 * @param drawableId 分割线图片 */ public RecycleViewDivider(Context context, int orientation, int drawableId) { this(context, orientation); mDivider = ContextCompat.getDrawable(context, drawableId); mDividerHeight = mDivider.getIntrinsicHeight(); } /** * 自定义分割线 * * @param context 上下文 * @param orientation 列表方向 * @param dividerHeight 分割线高度 * @param dividerColor 分割线颜色 */ public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) { this(context, orientation); mDividerHeight = dividerHeight; mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(dividerColor); mPaint.setStyle(Paint.Style.FILL); } /** * 获取分割线尺寸 * * @param outRect 线的矩框 * @param view 线 * @param parent RecyclerView * @param state 状态 */ @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state){ super.getItemOffsets(outRect, view, parent, state); //TODO if (mOrientation == LinearLayoutManager.VERTICAL) { outRect.set(0, 0, 0, mDividerHeight);//横线矩框 } else { outRect.set(0, 0, mDividerHeight, 0);//竖线矩框 } } /** * 绘制分割线 * * @param c 画布 * @param parent RecyclerView * @param state 状态 */ @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); if (mOrientation == LinearLayoutManager.VERTICAL) { drawVertical(c, parent);//横线矩框 } else { drawHorizontal(c, parent);//竖线矩框 } } /** * 绘制纵向列表时的分隔线 这时分隔线是横着的 * 每次 left相同,top根据child变化,right相同,bottom也变化 * * @param canvas 画布 * @param parent RecyclerView */ private void drawVertical(Canvas canvas, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getMeasuredWidth() - parent.getPaddingRight(); final int childSize = parent.getChildCount(); for (int i = 0; i < childSize; i++) { final View child = parent.getChildAt(i); RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getBottom() + layoutParams.bottomMargin; final int bottom = top + mDividerHeight; if (mDivider != null) { mDivider.setBounds(left, top, right, bottom); mDivider.draw(canvas); } if (mPaint != null) { canvas.drawRect(left, top, right, bottom, mPaint); } } } /** * 绘制横向列表时的分隔线 这时分隔线是竖着的 * l、r 变化; t、b 不变 * * @param canvas 画布 * @param parent RecyclerView */ private void drawHorizontal(Canvas canvas, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom(); final int childSize = parent.getChildCount(); for (int i = 0; i < childSize; i++) { final View child = parent.getChildAt(i); RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + layoutParams.rightMargin; final int right = left + mDividerHeight; if (mDivider != null) { mDivider.setBounds(left, top, right, bottom); mDivider.draw(canvas); } if (mPaint != null) { canvas.drawRect(left, top, right, bottom, mPaint); } } } }[1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力
更多安卓技术欢迎访问:安卓技术栈 我的github地址:欢迎star 简书首发,腾讯云+社区同步更新张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
QQ:1981462002 邮箱:1981462002@qq.com 微信:zdl1994328
转载于:https://www.cnblogs.com/toly-top/p/9781914.html
相关资源:JAVA上百实例源码以及开源项目