Skip to content

Instantly share code, notes, and snippets.

@NaturalizerINA
Created July 6, 2019 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NaturalizerINA/eaeb04e1b1e2cc1a4d732eeeb09476f9 to your computer and use it in GitHub Desktop.
Save NaturalizerINA/eaeb04e1b1e2cc1a4d732eeeb09476f9 to your computer and use it in GitHub Desktop.
This is the way, how to make grid decoration
//the function of this class is for customing and creating padding or margin for the recyclerview
public class RecyclerViewMargin extends RecyclerView.ItemDecoration {
//column is for accomodate the number of columns
int columns;
//margin is for accomodate the size of margin
int margin;
public RecyclerViewMargin(int margin, int columns) {
//passing the margin from margin that defined outside to inside
this.margin = margin;
//passing the columns from column that defined outside to inside
this.columns = columns;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//get the position to determine the condition of decoration
int position = parent.getChildLayoutPosition(view);
// == Right and Left margin==
outRect.right = margin;
//if the position is even(genap) then add left margin
//(when position in 0, 2, 4, 6, 8) give left margin, for example 0 / 2 = a -> 0 = 2 x a, then a is 0 to get the 0 result in the left (0 = ..)
if (position % columns == 0) outRect.left = margin;
// == Right and Left margin==
// == Top and Bottom margin==
//if the position is in top or in 0 or 1 position then add the top margins
if (position < columns) outRect.top = margin;
outRect.bottom = margin;
// == Top and Bottom margin==
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment