RecyclerView自定义ItemDecoration
来源:互联网 发布:淘宝卖家学院光云科技 编辑:程序博客网 时间:2024/05/01 11:40
从ItemDecoration的源码:
public static abstract class ItemDecoration { /** * 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. * * @param c Canvas to draw into * @param parent RecyclerView this ItemDecoration is drawing into * @param state The current state of RecyclerView */ public void onDraw(Canvas c, RecyclerView parent, State state) { onDraw(c, parent); } /** * @deprecated * Override {@link #onDraw(Canvas, RecyclerView, RecyclerView.State)} */ @Deprecated public void onDraw(Canvas c, RecyclerView parent) { } /** * 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. * * @param c Canvas to draw into * @param parent RecyclerView this ItemDecoration is drawing into * @param state The current state of RecyclerView. */ public void onDrawOver(Canvas c, RecyclerView parent, State state) { onDrawOver(c, parent); } /** * @deprecated * Override {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)} */ @Deprecated public void onDrawOver(Canvas c, RecyclerView parent) { } /** * @deprecated * Use {@link #getItemOffsets(Rect, View, RecyclerView, State)} */ @Deprecated public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { outRect.set(0, 0, 0, 0); } /** * Retrieve any offsets for the given item. Each field of <code>outRect</code> 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. * * <p> * If this ItemDecoration does not affect the positioning of item views, it should set * all four fields of <code>outRect</code> (left, top, right, bottom) to zero * before returning. * * <p> * If you need to access Adapter for additional data, you can call * {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the * View. * * @param outRect Rect to receive the output. * @param view The child view to decorate * @param parent RecyclerView this ItemDecoration is decorating * @param state The current state of RecyclerView. */ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) { getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(), parent); }}可以知道:
而我们定制Item 分割线真正要用到的方法其实只有两个,
1、画分割线 public void onDraw(Canvas c, RecyclerView parent, State state) {
}
2、设置分割线的size public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
}
public class DividerItemDecoration extends RecyclerView.ItemDecoration{ public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; /** * RecyclerView的布局方向,默认先赋值 * 为纵向布局 * RecyclerView 布局可横向,也可纵向 * 横向和纵向对应的分割想画法不一样 * */ private int mOrientation; /** * item之间分割线的size,默认为1 */ private int mItemSize = 1 ; /** * 绘制item分割线的画笔,和设置其属性 * 来绘制个性分割线 */ private Paint mPaint ; /** * 构造方法传入布局方向,不可不传 * @param context * @param orientation */ public DividerItemDecoration(Context context, int orientation) { setOrientation(orientation); mItemSize = (int) TypedValue.applyDimension(mItemSize, TypedValue.COMPLEX_UNIT_DIP,context.getResources().getDisplayMetrics()); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ; mPaint.setColor(Color.BLUE); /*设置填充*/ mPaint.setStyle(Paint.Style.FILL); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {// Log.v("recyclerview - itemdecoration", "onDraw()"); if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } //绘制垂直分割线 public void drawVertical(Canvas canvas, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext()); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mItemSize; canvas.drawRect(left,top,right,bottom,mPaint); } } //绘制水平分割线 public void drawHorizontal(Canvas canvas, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mItemSize ; canvas.drawRect(left,top,right,bottom,mPaint); } } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mItemSize); } else { outRect.set(0, 0, mItemSize, 0); } }}
使用的时候
layoutManager=new LinearLayoutManager(mContext);mRecyclerView.setLayoutManager(layoutManager);//添加分割线mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));// 设置item动画mRecyclerView.setItemAnimator(new DefaultItemAnimator());mRecyclerView.setAdapter(mRecyAdapter);
0 0
- 自定义 RecyclerView.ItemDecoration
- recyclerView-自定义itemDecoration详解
- RecyclerView自定义ItemDecoration
- RecyclerView高级应用——自定义ItemDecoration
- RecyclerView.ItemDecoration
- 使用ItemDecoration自定义RecyclerView的分割线实现头布局
- Android 自定义RecyclerView.ItemDecoration(GridLayoutManager布局下)
- RecyclerView.ItemDecoration 使用总结
- RecyclerView之ItemDecoration详解
- RecyclerView.ItemDecoration(笔记)
- RecyclerView中itemDecoration
- RecyclerView之ItemDecoration详解
- RecyclerView机制解析: ItemDecoration
- RecyclerView之ItemDecoration
- RecyclerView之ItemDecoration由浅入深
- RecyclerView之ItemDecoration详解
- RecyclerView ItemDecoration 学习
- RecyclerView之ItemDecoration由浅入深
- React和Less错误解决集
- java概述
- Apache Ambari 01 - Ambari Mirror 安装部署
- 子集生成
- 虚拟机找不到/mnt/hgfs挂载目录
- RecyclerView自定义ItemDecoration
- mysql启动失败:mysql服务无法启动 服务没有报告任何错误 排查过程及解决方法
- Dumb-Init进程信号处理
- Android HTTP操作(三)
- iOS 开发command+左键 不能索引 出现问号 Xcode一直处于indexing状态
- 一些有用的SAP技术TCODE
- 交换两个数据的方法
- pcntl_fock & pcntl_waitpid 初学
- iOS知识点八