package com.dev.bins.recyclerviewitemdecoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.SoundEffectConstants;
import android.view.View;

import java.util.List;

/**
 * Created by bin on 02/01/2017.
 */

public class SuspendingDecoration extends RecyclerView.ItemDecoration {
    private List<CityBean> mDatas;
    private int height = 50;

    public SuspendingDecoration(List<CityBean> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int pos = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        if (pos == 0) {
            outRect.set(0, height, 0, 0);
        } else if (!mDatas.get(pos).getTag().equals(mDatas.get(pos - 1).getTag())) {
            outRect.set(0, height, 0, 0);
        } else {
            outRect.set(0, 0, 0, 0);
        }
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(20);
        paint.setColor(Color.GRAY);
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setDither(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(20);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(50);
        for (int i = 0; i < parent.getChildCount(); i++) {
            View childAt = parent.getChildAt(i);
            int pos = ((RecyclerView.LayoutParams) childAt.getLayoutParams()).getViewLayoutPosition();
            if (i == 0) {
                c.drawRect(0, childAt.getTop() - 50, parent.getWidth(), childAt.getTop(), paint);
                c.drawText(mDatas.get(pos).getTag(), 10, childAt.getTop() - 10, textPaint);
            } else if (!mDatas.get(pos).getTag().equals(mDatas.get(pos - 1).getTag())) {
                c.drawRect(0, childAt.getTop() - 50, parent.getWidth(), childAt.getTop(), paint);
                c.drawText(mDatas.get(pos).getTag(), 10, childAt.getTop() - 10, textPaint);
            }
        }
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(20);
        paint.setColor(Color.GRAY);
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setDither(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(20);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(50);

        int pos = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
        View view = parent.findViewHolderForLayoutPosition(pos).itemView;
        c.save();
        if (!mDatas.get(pos).getTag().equals(mDatas.get(pos+1).getTag())){
            if (view.getBottom() < height) {//view.getTop() + view.getHeight()和getbottom的效果是一样的
                System.out.println(view.getTop());
                System.out.println(view.getHeight());
                c.translate(0, view.getTop() + view.getHeight() - height);
            }
        }
        c.drawRect(0, parent.getTop(), parent.getWidth(), parent.getTop()+height, paint);
        c.drawText(mDatas.get(pos).getTag(), 10, 35, textPaint);
        c.restore();

    }
}

@@ -272,50 +272,8 @@
 ew;%0A
-import android.view.SoundEffectConstants;%0A
 impo
@@ -496,16 +496,72 @@
 ht = 50;
+%0A    private Paint mPaint;%0A    private Paint mTextPaint;
 %0A%0A    pu
@@ -637,24 +637,569 @@
 s = mDatas;%0A
+        initPaint();%0A    %7D%0A%0A%0A    private void initPaint() %7B%0A        mPaint = new Paint();%0A        mPaint.setAntiAlias(true);%0A        mPaint.setDither(true);%0A        mPaint.setStyle(Paint.Style.FILL);%0A        mPaint.setStrokeWidth(20);%0A        mPaint.setColor(Color.GRAY);%0A        mTextPaint = new Paint();%0A        mTextPaint.setAntiAlias(true);%0A        mTextPaint.setDither(true);%0A        mTextPaint.setStyle(Paint.Style.FILL);%0A        mTextPaint.setStrokeWidth(20);%0A        mTextPaint.setColor(Color.BLACK);%0A        mTextPaint.setTextSize(50);%0A
     %7D%0A%0A    @
@@ -1873,491 +1873,8 @@
 e);%0A
-        Paint paint = new Paint();%0A        paint.setAntiAlias(true);%0A        paint.setDither(true);%0A        paint.setStyle(Paint.Style.FILL);%0A        paint.setStrokeWidth(20);%0A        paint.setColor(Color.GRAY);%0A        Paint textPaint = new Paint();%0A        textPaint.setAntiAlias(true);%0A        textPaint.setDither(true);%0A        textPaint.setStyle(Paint.Style.FILL);%0A        textPaint.setStrokeWidth(20);%0A        textPaint.setColor(Color.BLACK);%0A        textPaint.setTextSize(50);
 %0A   
@@ -2189,33 +2189,34 @@
 ildAt.getTop(), 
-p
+mP
 aint);%0A         
@@ -2278,33 +2278,34 @@
 .getTop() - 10, 
-t
+mT
 extPaint);%0A     
@@ -2478,17 +2478,18 @@
 tTop(), 
-p
+mP
 aint);%0A 
@@ -2567,17 +2567,18 @@
 ) - 10, 
-t
+mT
 extPaint
@@ -2761,492 +2761,8 @@
 );%0A%0A
-        Paint paint = new Paint();%0A        paint.setAntiAlias(true);%0A        paint.setDither(true);%0A        paint.setStyle(Paint.Style.FILL);%0A        paint.setStrokeWidth(20);%0A        paint.setColor(Color.GRAY);%0A        Paint textPaint = new Paint();%0A        textPaint.setAntiAlias(true);%0A        textPaint.setDither(true);%0A        textPaint.setStyle(Paint.Style.FILL);%0A        textPaint.setStrokeWidth(20);%0A        textPaint.setColor(Color.BLACK);%0A        textPaint.setTextSize(50);%0A
 %0A   
@@ -3009,17 +3009,19 @@
 .get(pos
-+
+ + 
 1).getTa
@@ -3025,16 +3025,17 @@
 tTag()))
+ 
 %7B%0A      
@@ -3405,17 +3405,19 @@
 op()
-+
+ + 
 height, 
 pain
@@ -3412,17 +3412,18 @@
 height, 
-p
+mP
 aint);%0A 
@@ -3474,17 +3474,18 @@
 10, 35, 
-t
+mT
 extPaint
@@ -3515,10 +3515,12 @@
 %0A%0A    %7D%0A
+%0A%0A
 %7D%0A

refactor paint