RecyclerView 的ItemDecoration 类似于时间轴

package com.gemry.seneschal;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MyItemDecoration extends RecyclerView.ItemDecoration {

    Context mContext;
    Paint mCirclePaint;
    Paint mTextPaint;
    Paint mPaint;
    private int radius = 10;
    private int dividerHeight = 4;
    private int leftGrap = 10;
    private int circlePaintWidth = 6;

    public MyItemDecoration(Context context) {
        mContext = context;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor("#E5E5E5"));
        mPaint.setStyle(Paint.Style.FILL);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.parseColor("#ff0000"));
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(26);
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(Color.parseColor("#7F57DB"));
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(circlePaintWidth);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        int itemPosition = parent.getChildAdapterPosition(view);
//        if (itemPosition != 0) {
        outRect.set(160, 0, 0, 0);
//        }
    }

    @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 child = parent.getChildAt(i);
            RecyclerView.LayoutParams
                    lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            int index = parent.getChildAdapterPosition(child);
            float left = (child.getLeft() - lp.leftMargin) / 2 - dividerHeight / 2;
            float right = left + dividerHeight;
            float top = child.getTop() - lp.topMargin;
            float bottom = (child.getBottom() + lp.bottomMargin - top) / 2 - radius + top;
//            if (i != 0 ){
//
//            }
            c.drawRect(left, top, right, bottom, mPaint);
            c.drawCircle((child.getLeft() - lp.leftMargin) / 2, bottom + radius + circlePaintWidth / 2, radius, mCirclePaint);
            c.drawRect(left, bottom + 2 * radius  + circlePaintWidth, right, bottom + 2 * radius + bottom - top, mPaint);

            c.drawText("20:00", (child.getLeft() - lp.leftMargin) / 2 + leftGrap + radius, bottom + 2*radius + circlePaintWidth / 2, mTextPaint);
        }
//        //下边界
//        for (int i = 0; i < childCount; i++) {
//            View child = parent.getChildAt(i);
//
//            RecyclerView.LayoutParams
//                    lp = (RecyclerView.LayoutParams) child.getLayoutParams();
//            int index = parent.getChildAdapterPosition(child);
            if (index == 0) {
                continue;
            }
//            float left = parent.getPaddingLeft();
//
            Log.e("TAG","-----left---" + child.getLeft());
//            float right = parent.getWidth() - parent.getPaddingRight();
//            float top = child.getBottom() + lp.bottomMargin;
//            float bottom = top + 10;
//
//            c.drawRect(left, top, right, bottom, mPaint);
//        }
//
//        //左边界
//        for (int i = 0; i < childCount; i++) {
//            View child = parent.getChildAt(i);
//
//            RecyclerView.LayoutParams
//                    lp = (RecyclerView.LayoutParams) child.getLayoutParams();
//            int index = parent.getChildAdapterPosition(child);
            if (index == 0) {
                continue;
            }
//            float left = lp.leftMargin;
//
//            Log.e("TAG", "-----left---" + child.getLeft());
//            float right = left + 10;
//            float top = child.getTop();
//            float bottom = top + 20;
//            Log.e("TAG", "-----left---" + child.getLeft() + "---lp.topMargin--" + lp.topMargin);
//            c.drawRect(left, top, right, bottom, mPaint);
//        }
//        //右边界
//        for (int i = 0; i < childCount; i++) {
//            View child = parent.getChildAt(i);
//
//            RecyclerView.LayoutParams
//                    lp = (RecyclerView.LayoutParams) child.getLayoutParams();
//            int index = parent.getChildAdapterPosition(child);
            if (index == 0) {
                continue;
            }
//            float left = child.getRight();
//
//            Log.e("TAG", "-----left---" + child.getLeft());
//            float right = left + 10;
//            float top = child.getTop();
//            float bottom = child.getBottom() + 10;
//            Log.e("TAG", "-----left---" + child.getLeft() + "---lp.topMargin--" + lp.topMargin);
//            c.drawRect(left, top, right, bottom, mPaint);
//        }
上边界
//        for (int i = 0; i < childCount; i++) {
//            View child = parent.getChildAt(i);
//
//            RecyclerView.LayoutParams
//                    lp = (RecyclerView.LayoutParams) child.getLayoutParams();
//            int index = parent.getChildAdapterPosition(child);
            if (index == 0) {
                continue;
            }
//            float left = lp.leftMargin;
//
//            Log.e("TAG", "-----left---" + child.getLeft());
//            float right = child.getRight() + 10;
//            float top = child.getTop() - 10 - lp.topMargin;
//            float bottom = top + 10;
//            Log.e("TAG", "-----left---" + child.getLeft() + "---lp.topMargin--" + lp.topMargin);
//            c.drawRect(left, top, right, bottom, mPaint);
//        }
    }
}

你可能感兴趣的:(RecyclerView 的ItemDecoration 类似于时间轴)