首页 > 学院 > 开发设计 > 正文

Recyclerview使用flexibledivider处理分割线

2019-11-09 19:06:24
字体:
来源:转载
供稿:网友

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());**

简单的

//设置颜色、宽度、边距RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);recyclerView.addItemDecoration( new HorizontalDividerItemDecoration.Builder(this) .color(Color.RED) .sizeResId(R.dimen.divider) .marginResId(R.dimen.leftmargin, R.dimen.rightmargin) .build());//设置颜色、宽度、画笔的样式(空心实心 间隔线)Paint paint = new Paint();paint.setStrokeWidth(5);paint.setColor(Color.BLUE);paint.setAntiAlias(true);paint.setPathEffect(new DashPathEffect(new float[]{25.0f, 25.0f}, 0));recyclerView.addItemDecoration( new HorizontalDividerItemDecoration.Builder(this).paint(paint).build());//设置宽度、支持.9图片RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this) .drawable(R.drawable.sample) .size(15) .build());
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表