daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Apr 24th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.content.Context;
  2. import android.graphics.Rect;
  3. import android.support.annotation.DimenRes;
  4. import android.support.v7.widget.LinearLayoutManager;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.View;
  7.  
  8. import com.showroomtechnologies.showroom.R;
  9.  
  10. public class SpacingItemDecoration extends RecyclerView.ItemDecoration {
  11.  
  12.     private final int paddingLeft;
  13.     private final int paddingTop;
  14.     private final int paddingRight;
  15.     private final int paddingBottom;
  16.  
  17.     public SpacingItemDecoration(Context context) {
  18.         this(context, R.dimen.normal);
  19.     }
  20.  
  21.     public SpacingItemDecoration(Context context, @DimenRes int padding) {
  22.         this(context, padding, padding, padding, padding);
  23.     }
  24.  
  25.     public SpacingItemDecoration(Context context,
  26.                                  @DimenRes int paddingLeft,
  27.                                  @DimenRes int paddingTop,
  28.                                  @DimenRes int paddingRight,
  29.                                  @DimenRes int paddingBottom) {
  30.         this.paddingLeft = (int) context.getResources().getDimension(paddingLeft);
  31.         this.paddingTop = (int) context.getResources().getDimension(paddingTop);
  32.         this.paddingRight = (int) context.getResources().getDimension(paddingRight);
  33.         this.paddingBottom = (int) context.getResources().getDimension(paddingBottom);
  34.     }
  35.  
  36.     @Override
  37.     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  38.         super.getItemOffsets(outRect, view, parent, state);
  39.         if (!(parent.getLayoutManager() instanceof LinearLayoutManager)) throw new IllegalArgumentException("You must use LinearLayoutManager");
  40.         outRect.left = paddingLeft / 2;
  41.         outRect.right = paddingRight / 2;
  42.         outRect.top = paddingTop / 2;
  43.         outRect.bottom = paddingBottom / 2;
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top