recyclerView= (RecyclerView) findViewById(R.id.rc_goods); //添加分割线 recyclerView.addItemDecoration(new Rc_ItemDecoration(search_homeActivity.this)); //布局管理器 LinearLayoutManager llm=new LinearLayoutManager(this); recyclerView.setLayoutManager(llm);recyclerView.setAdapter(mda);
//先写这些public class Rc_ItemDecoration extends RecyclerView.ItemDecoration { Context con; private Paint mPaint; private float mDividerHeight; public Rc_ItemDecoration(Context con) { this.con=con; mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.parseColor("#CDC5BF")); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); int childCount = parent.getChildCount(); for ( int i = 0; i < childCount; i++ ) { View view = parent.getChildAt(i); int index = parent.getChildAdapterPosition(view); //第一个ItemView不需要绘制 if ( index == 0 ) { continue; } float dividerTop = view.getTop() - mDividerHeight; float dividerLeft = parent.getPaddingLeft(); float dividerBottom = view.getTop(); float dividerRight = parent.getWidth() - parent.getPaddingRight(); c.drawRect(dividerLeft,dividerTop,dividerRight,dividerBottom,mPaint); } } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(c, parent, state); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); if (parent.getChildAdapterPosition(view) != 0){ //这里直接硬编码为1px可以改分割线粗细 outRect.top = 20; outRect.left=15; outRect.right=15; mDividerHeight = 20; } } }
RecyclerView 的添加分割线
最新推荐文章于 2019-10-29 16:07:45 发布