天天看点

用ItemDecoration实现物流时间轴

1、效果图

用ItemDecoration实现物流时间轴

2、TimelineDecoration

自定义ItemDecoration ,绘制虚线和圆点;

public class TimelineDecoration extends RecyclerView.ItemDecoration {
    private int leftWidth;//时间轴宽度
    private int marginTop;//圆距离item顶部高度
    private int dividerHeight;//线条粗细
    private int ovalRadius = 12;//灰色圆的半径

    private Paint linePaint;
    private Paint circlePaint;

    public TimelineDecoration(int leftWidth, int marginTop, int dividerHeight, int lineColor) {
        this.leftWidth = leftWidth;
        this.marginTop = marginTop;
        this.dividerHeight = dividerHeight;
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(lineColor);
        //设置画直线格式
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);
        //设置虚线效果
        linePaint.setPathEffect(new DashPathEffect(new float[]{15f, 10f}, 0));
        linePaint.setStrokeWidth(2);//设置画笔宽度 ,单位px

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(lineColor);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(leftWidth, 0, 0, dividerHeight);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            int top = child.getTop();
            int bottom = child.getBottom();
            int left = parent.getPaddingLeft() + leftWidth / 2;
            //圆点起始位置
            int ovalCenterY = top + marginTop + ovalRadius;

            //竖直线 https://blog.csdn.net/qq_33210042/article/details/105187871
            Path linePath = new Path();
            if (i == 0) {
                linePath.moveTo(left, ovalCenterY);
                linePath.lineTo(left + dividerHeight, bottom + dividerHeight);
            } else if (i == childCount - 1) {
                linePath.moveTo(left, top);
                linePath.lineTo(left + dividerHeight, ovalCenterY + dividerHeight);
            } else {
                linePath.moveTo(left, top);
                linePath.lineTo(left + dividerHeight, bottom + dividerHeight);
            }
            c.drawPath(linePath, linePaint);

            //绘制小圆点
            c.drawCircle(left, ovalCenterY, ovalRadius, circlePaint);

            Log.d("caowj", left + "," + top + "," + bottom + "," + ovalCenterY);

            //横向分割线
            c.drawLine(parent.getPaddingLeft() + leftWidth, bottom, parent.getWidth() - parent.getPaddingRight(), bottom + dividerHeight, linePaint);
        }
    }
}
           

3、调用方式

RecyclerView正常设置addItemDecoration();

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        BaseQuickAdapter<String,BaseViewHolder> adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_transport,
                Arrays.asList(getResources().getStringArray(R.array.transport_list))) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                TextView tvDescribe = helper.getView(R.id.tv_describe);
                TextView tvTime = helper.getView(R.id.tv_time);
                tvDescribe.setText(item);
                tvTime.setText("2018-06-01 12:00");

                int position = helper.getAdapterPosition();
                tvDescribe.setTextColor(position==0?0xff4caf65:0xff999999);
                tvTime.setTextColor(position==0?0xff4caf65:0xff999999);
            }
        };

        recyclerView.addItemDecoration(new TimelineDecoration(getDimen(R.dimen.time_line_width),
                getDimen(R.dimen.time_line_top), 1, getResources().getColor(R.color.lineColor)));
        recyclerView.setAdapter(adapter);
    }

    private int getDimen(int dimeRes){
        return (int) getResources().getDimension(dimeRes);
    }
}
           

参考:https://www.jianshu.com/p/64eb1fbea2d5

继续阅读