Guest User

Untitled

a guest
Feb 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import android.content.Context;
  2. import android.graphics.Rect;
  3. import android.support.annotation.DimenRes;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.View;
  7.  
  8. /**
  9. * Created by wrewolf on 13.02.18.
  10. */
  11.  
  12. public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
  13.  
  14. private int columnCount;
  15. private int f_spasing;
  16. private int h_spasing;
  17.  
  18. public GridSpacingItemDecoration(@NonNull Context context, @DimenRes int itemOffsetId, @NonNull int columnCount) {
  19. this(context.getResources().getDimensionPixelSize(itemOffsetId), columnCount);
  20. }
  21.  
  22. public GridSpacingItemDecoration(int spacing, int columnCount) {
  23. this.columnCount = columnCount;
  24. this.f_spasing = spacing;
  25. this.h_spasing = spacing / 2;
  26. }
  27.  
  28. @Override
  29. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  30. super.getItemOffsets(outRect, view, parent, state);
  31. int position = parent.getChildAdapterPosition(view); // item position
  32.  
  33. outRect.top = 0;
  34. outRect.left = 0;
  35. outRect.right = 0;
  36. outRect.bottom = 0;
  37. if (position > 0) {
  38. outRect.top = f_spasing;
  39. if (this.columnCount == 2) {
  40. if (position % 2 == 0) {
  41. outRect.left = h_spasing;
  42. outRect.right = f_spasing;
  43. } else {
  44. outRect.left = f_spasing;
  45. outRect.right = h_spasing;
  46. }
  47. } else {
  48. if (position % columnCount == 0) {
  49. outRect.left = h_spasing;
  50. outRect.right = f_spasing;
  51. } else if (position % columnCount == 1) {
  52. outRect.left = f_spasing;
  53. outRect.right = h_spasing;
  54. } else {
  55. outRect.left = h_spasing;
  56. outRect.right = h_spasing;
  57. }
  58. }
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment