ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RecyclerView 的ItemDecoration 类似于时间轴

2019-05-07 19:53:42  阅读:263  来源: 互联网

标签:parent float 时间轴 lp child RecyclerView ItemDecoration left


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);
//        }
    }
}

标签:parent,float,时间轴,lp,child,RecyclerView,ItemDecoration,left
来源: https://blog.csdn.net/xuyao625010693/article/details/89927645

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有