package com.zhilin.patrol.adapter.helper;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 *
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {

    private int spaceSize;

    public GridItemDecoration(int spaceSize) {
        this.spaceSize = spaceSize;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
//        boolean b = state.willRunPredictiveAnimations();
        int spanCount = layoutManager.getSpanCount();
        int column = position % spanCount; // item column

        int left = spaceSize - column * spaceSize / spanCount;
        int right = (column + 1) * spaceSize / spanCount;
        int top = spaceSize;

        outRect.set(left, top, right, 0);
    }

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

}
