Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. package com.innercirclesoftware.reddit.shared.ui.recyclerview.decorations;
  2.  
  3. import android.graphics.Rect;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.View;
  7.  
  8. public class MarginItemDecoration extends RecyclerView.ItemDecoration {
  9.  
  10.     private final int columnCount;
  11.     private final int margin;
  12.  
  13.     public MarginItemDecoration(int margin, int columnCount) {
  14.         this.columnCount = columnCount;
  15.         this.margin = margin;
  16.     }
  17.  
  18.     @Override
  19.     public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
  20.         super.getItemOffsets(outRect, view, parent, state);
  21.  
  22.         int position = parent.getChildAdapterPosition(view);
  23.  
  24.         boolean firstInRow = position % columnCount == 0;
  25.         boolean lastInRow = position % columnCount == columnCount - 1;
  26.         boolean isInLastRow = position > state.getItemCount() - columnCount - 1;
  27.  
  28.         //top
  29.         outRect.top = margin;
  30.  
  31.         //left
  32.         if (firstInRow) outRect.left = margin;
  33.         else outRect.left = margin / 2;
  34.  
  35.         //right
  36.         if (lastInRow) outRect.right = margin;
  37.         else outRect.right = margin / 2;
  38.  
  39.         //bottom
  40.         if (isInLastRow) outRect.bottom = margin;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement