package com.gh.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.R;

public class VerticalItemDecoration extends RecyclerView.ItemDecoration {

	private Paint paint;
	private int mInterval = 0;

	public VerticalItemDecoration(Context context, int interval) {
		paint = new Paint();
		paint.setColor(context.getResources().getColor(R.color.background));
		mInterval = DisplayUtils.dip2px(context, interval);
	}

	@Override
	public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
		int left = parent.getPaddingLeft();
		int right = parent.getMeasuredWidth() - parent.getPaddingRight();
		for (int i = 0, size = parent.getChildCount() - 1; i < size; i++) {
			View child = parent.getChildAt(i);
			RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child
					.getLayoutParams();
			int top = child.getBottom() + layoutParams.bottomMargin;
			int bottom = top + mInterval;
			c.drawRect(left, top, right, bottom, paint);
		}
	}

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