Recyclerview使用flexibledivider处理分割线,如分割线的宽度,颜色,边距等,可以设置水平或竖直方向。 依赖库compile ‘com.yqritc:recyclerview-flexibledivider:1.4.0’ 使用方式:
让自己的Adapter implements FlexibleDividerDecoration. PaintPRovider, HorizontalDividerItemDecoration. MarginProvider等,会需要复写几个方法来控制分割线的各种属性。
@Override public int dividerLeftMargin(int position, RecyclerView parent) { if (position == 3){ return 0; } return (int) (90*mHeight1px); } @Override public int dividerRightMargin(int position, RecyclerView parent) { return 0; } @Override public Paint dividerPaint(int position, RecyclerView parent) { Paint paint = new Paint(); switch (position) { case 5: case 3: int blank = ContextCompat.getColor(mContext, R.color.ref_bg); paint.setColor(blank); paint.setStrokeWidth(16*mHeight1px); break; default: int color = ContextCompat.getColor(mContext, R.color.divider); paint.setColor(color); paint.setAntiAlias(true); paint.setStrokeWidth(1*mHeight1px); break; } return paint; }在adapter调用的地方
mAdapter = new MyAdapter(context,mList);LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);recyclerview.setLayoutManager(linearLayoutManager);recyclerview.setAdapter(mAdapter);recyclerview.addItemDecoration(new **HorizontalDividerItemDecoration.Builder(getContext()) .paintProvider(mAdapter) .marginProvider(mAdapter) .build());**新闻热点
疑难解答