Advertisement
Guest User

Untitled

a guest
May 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import android.graphics.Canvas;
  2. import android.graphics.Paint;
  3. import android.graphics.Rect;
  4. import android.support.annotation.ColorInt;
  5. import android.support.v4.view.ViewCompat;
  6. import android.support.v7.widget.RecyclerView;
  7. import android.view.View;
  8.  
  9. public class DividerItemDecoration extends RecyclerView.ItemDecoration {
  10.  
  11. public static final int HORIZONTAL = 1;
  12. public static final int VERTICAL = 2;
  13. public static final int BOTH = 3;
  14.  
  15. private Paint paint;
  16. private int dividerHeight;
  17. private int orientation;
  18.  
  19. private DividerItemDecoration() {
  20. paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  21. paint.setStyle(Paint.Style.FILL);
  22. }
  23.  
  24. public void setDividerHeight(int dividerHeight) {
  25. this.dividerHeight = dividerHeight;
  26. }
  27.  
  28. public void setColor(int color) {
  29. paint.setColor(color);
  30. }
  31.  
  32. public int getOrientation() {
  33. return orientation;
  34. }
  35.  
  36. public void setOrientation(int orientation) {
  37. this.orientation = orientation;
  38. }
  39.  
  40. @Override
  41. public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
  42. if (orientation == VERTICAL) {
  43. drawVertical(c, parent);
  44. } else if (orientation == HORIZONTAL) {
  45. drawHorizontal(c, parent);
  46. } else if (orientation == BOTH) {
  47. drawVertical(c, parent);
  48. drawHorizontal(c, parent);
  49. }
  50. }
  51.  
  52. protected void drawVertical(Canvas c, RecyclerView parent) {
  53. final int left = parent.getPaddingLeft();
  54. final int right = parent.getWidth() - parent.getPaddingRight();
  55. final int childCount = parent.getChildCount();
  56. for (int i = 0; i < childCount; i++) {
  57. final View child = parent.getChildAt(i);
  58. if (parent.getChildAdapterPosition(child) == (parent.getAdapter().getItemCount() - 1)) {
  59. continue;
  60. }
  61. final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
  62. final int bottom;
  63. final int top;
  64. top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationY(child));
  65. bottom = top + dividerHeight;
  66. c.drawRect(left, top, right, bottom, paint);
  67. }
  68. }
  69.  
  70. protected void drawHorizontal(Canvas c, RecyclerView parent) {
  71. final int top = parent.getPaddingTop();
  72. final int bottom = parent.getHeight() - parent.getPaddingBottom();
  73. final int childCount = parent.getChildCount();
  74. for (int i = 0; i < childCount; i++) {
  75. final View child = parent.getChildAt(i);
  76. if (parent.getChildAdapterPosition(child) == (parent.getAdapter().getItemCount() - 1)) {
  77. continue;
  78. }
  79. final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
  80. .getLayoutParams();
  81. final int right;
  82. final int left;
  83. left = child.getRight() + params.rightMargin + Math.round(ViewCompat.getTranslationX(child));
  84. right = left + dividerHeight;
  85. c.drawRect(left, top, right, bottom, paint);
  86. }
  87. }
  88.  
  89. @Override
  90. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  91. if (orientation == VERTICAL) {
  92. outRect.set(0, 0, 0, dividerHeight);
  93. } else if (orientation == HORIZONTAL){
  94. outRect.set(0, 0, dividerHeight, 0);
  95. } else if (orientation == BOTH) {
  96. outRect.set(0, 0, dividerHeight, dividerHeight);
  97. }
  98. }
  99.  
  100. public static class Builder {
  101.  
  102. private int dividerHeight;
  103. private int color;
  104. private int orientation;
  105.  
  106. public Builder dividerHeight(int dividerHeight) {
  107. this.dividerHeight = dividerHeight;
  108. return this;
  109. }
  110.  
  111. public Builder color(@ColorInt int color) {
  112. this.color = color;
  113. return this;
  114. }
  115.  
  116. public Builder orientation(int orientation) {
  117. this.orientation = orientation;
  118. return this;
  119. }
  120.  
  121. public DividerItemDecoration build() {
  122. DividerItemDecoration divider = new DividerItemDecoration();
  123.  
  124. if (dividerHeight == 0) {
  125. // Set default divider height to 1px.
  126. divider.setDividerHeight(1);
  127. } else if (dividerHeight > 0) {
  128. divider.setDividerHeight(dividerHeight);
  129. } else {
  130. throw new IllegalArgumentException("Divider's height can't be negative.");
  131. }
  132.  
  133. if (color == 0) {
  134. throw new IllegalArgumentException("Don't forget to set color");
  135. } else {
  136. divider.setColor(color);
  137. }
  138.  
  139. if (orientation != HORIZONTAL && orientation != VERTICAL && orientation != BOTH) {
  140. throw new IllegalArgumentException("Invalid orientation");
  141. } else {
  142. divider.setOrientation(orientation);
  143. }
  144.  
  145. return divider;
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement