package ch.pizzalink.android.view;

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

/**
 * Created by cimenmus on 20/09/2017.
 */

// This item decoration is for two column grid layout manager
public class GridSpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public GridSpacesItemDecoration(int space) {
        this.space = space;
    }

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

        outRect.left = space;
        outRect.bottom = space;

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0 ||  parent.getChildLayoutPosition(view) == 1)
            outRect.top = space;
        else
            outRect.top = 0;

        // Add right margin only for the first item in the line to avoid double space between items
        if (parent.getChildLayoutPosition(view) % 2 == 1)
            outRect.right = space;
        else
            outRect.right = 0;

    }
}
