Skip to content

Instantly share code, notes, and snippets.

@mrhether
Created August 24, 2015 22:01
Show Gist options
  • Save mrhether/70ef014109dae77b2827 to your computer and use it in GitHub Desktop.
Save mrhether/70ef014109dae77b2827 to your computer and use it in GitHub Desktop.
The following code will handle padding on StaggeredGridLayoutManager, GridLayoutManager, and LinearLayoutManager.
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by markhetherington on 15-08-17.
*/
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int halfSpace;
public SpacesItemDecoration(int space) {
this.halfSpace = space / 2;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getPaddingLeft() != halfSpace) {
parent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace);
parent.setClipToPadding(false);
}
outRect.top = halfSpace;
outRect.bottom = halfSpace;
outRect.left = halfSpace;
outRect.right = halfSpace;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment