Guest User

Untitled

a guest
Jan 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. public class GridDividerDecoration extends RecyclerView.ItemDecoration {
  2.  
  3. private static final int[] ATTRS = {android.R.attr.listDivider};
  4.  
  5. private Drawable mDivider;
  6. private int mInsets;
  7.  
  8. public GridDividerDecoration(Context context) {
  9. TypedArray a = context.obtainStyledAttributes(ATTRS);
  10. mDivider = a.getDrawable(0);
  11. a.recycle();
  12.  
  13. mInsets = context.getResources().getDimensionPixelSize(R.dimen.card_insets);
  14. }
  15.  
  16. @Override
  17. public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
  18. drawVertical(c, parent);
  19. drawHorizontal(c, parent);
  20. }
  21.  
  22. /**
  23. * Draw dividers at each expected grid interval
  24. */
  25. public void drawVertical(Canvas c, RecyclerView parent) {
  26. if (parent.getChildCount() == 0) return;
  27.  
  28. final int childCount = parent.getChildCount();
  29.  
  30. for (int i = 0; i < childCount; i++) {
  31. final View child = parent.getChildAt(i);
  32. final RecyclerView.LayoutParams params =
  33. (RecyclerView.LayoutParams) child.getLayoutParams();
  34.  
  35. final int left = child.getLeft() - params.leftMargin - mInsets;
  36. final int right = child.getRight() + params.rightMargin + mInsets;
  37. final int top = child.getBottom() + params.bottomMargin + mInsets;
  38. final int bottom = top + mDivider.getIntrinsicHeight();
  39. mDivider.setBounds(left, top, right, bottom);
  40. mDivider.draw(c);
  41. }
  42. }
  43.  
  44. /**
  45. * Draw dividers to the right of each child view
  46. */
  47. public void drawHorizontal(Canvas c, RecyclerView parent) {
  48. final int childCount = parent.getChildCount();
  49.  
  50. for (int i = 0; i < childCount; i++) {
  51. final View child = parent.getChildAt(i);
  52. final RecyclerView.LayoutParams params =
  53. (RecyclerView.LayoutParams) child.getLayoutParams();
  54.  
  55. final int left = child.getRight() + params.rightMargin + mInsets;
  56. final int right = left + mDivider.getIntrinsicWidth();
  57. final int top = child.getTop() - params.topMargin - mInsets;
  58. final int bottom = child.getBottom() + params.bottomMargin + mInsets;
  59. mDivider.setBounds(left, top, right, bottom);
  60. mDivider.draw(c);
  61. }
  62. }
  63.  
  64. @Override
  65. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  66. //We can supply forced insets for each item view here in the Rect
  67. outRect.set(mInsets, mInsets, mInsets, mInsets);
  68. }
  69. }
Add Comment
Please, Sign In to add comment