package com.veyndan.redditclient.ui.recyclerview.itemdecoration;

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

public class MarginItemDecoration extends RecyclerView.ItemDecoration {

    private final int margin;

    public MarginItemDecoration(@Px final int margin) {
        this.margin = margin;
    }

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

        outRect.bottom = margin;
        outRect.left = margin;
        outRect.right = margin;

        if (parent.getChildAdapterPosition(view) == 0) {
            outRect.top = margin;
        }
    }
}

@@ -584,69 +584,109 @@
     
-super.getItemOffsets(outRect, view, parent, state
+final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(
 );%0A
-%0A
         
 outR
@@ -685,197 +685,174 @@
     
-outRect.bottom = margin;%0A        outRect.left = margin;%0A        outRect.right = margin;%0A%0A        if (parent.getChildAdapterPosition(view) == 0) %7B%0A            outRect.top = margin;%0A        %7D
+final int position = params.getViewLayoutPosition();%0A        final int marginTop = position == 0 ? margin : 0;%0A        outRect.set(margin, marginTop, margin, margin);
 %0A   

Simplify and optimize getItemOffsets() logic