Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GridDividerItemDecoration.java « recycler « widget « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1dc7334c46e8dfeaaa22e9e26dc6d19864198d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.mapswithme.maps.widget.recycler;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Adds interior dividers to a RecyclerView with a GridLayoutManager.
 */
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration
{

  @NonNull
  private final Drawable mHorizontalDivider;
  @NonNull
  private final Drawable mVerticalDivider;
  private final int mNumColumns;

  /**
   * Sole constructor. Takes in {@link Drawable} objects to be used as
   * horizontal and vertical dividers.
   *
   * @param horizontalDivider A divider {@code Drawable} to be drawn on the
   *                          rows of the grid of the RecyclerView
   * @param verticalDivider   A divider {@code Drawable} to be drawn on the
   *                          columns of the grid of the RecyclerView
   * @param numColumns        The number of columns in the grid of the RecyclerView
   */
  public GridDividerItemDecoration(@NonNull Drawable horizontalDivider,
                                   @NonNull Drawable verticalDivider, int numColumns)
  {
    mHorizontalDivider = horizontalDivider;
    mVerticalDivider = verticalDivider;
    mNumColumns = numColumns;
  }

  /**
   * Draws horizontal and/or vertical dividers onto the parent RecyclerView.
   *
   * @param canvas The {@link Canvas} onto which dividers will be drawn
   * @param parent The RecyclerView onto which dividers are being added
   * @param state  The current RecyclerView.State of the RecyclerView
   */
  @Override
  public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state)
  {
    drawHorizontalDividers(canvas, parent);
    drawVerticalDividers(canvas, parent);
  }

  /**
   * Determines the size and location of offsets between items in the parent
   * RecyclerView.
   *
   * @param outRect The {@link Rect} of offsets to be added around the child view
   * @param view    The child view to be decorated with an offset
   * @param parent  The RecyclerView onto which dividers are being added
   * @param state   The current RecyclerView.State of the RecyclerView
   */
  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
  {
    super.getItemOffsets(outRect, view, parent, state);

    boolean childIsInLeftmostColumn = (parent.getChildAdapterPosition(view) % mNumColumns) == 0;
    if (!childIsInLeftmostColumn)
      outRect.left = mHorizontalDivider.getIntrinsicWidth();

    boolean childIsInFirstRow = (parent.getChildAdapterPosition(view)) < mNumColumns;
    if (!childIsInFirstRow)
      outRect.top = mVerticalDivider.getIntrinsicHeight();
  }

  /**
   * Adds horizontal dividers to a RecyclerView with a GridLayoutManager or
   * its subclass.
   *
   * @param canvas The {@link Canvas} onto which dividers will be drawn
   * @param parent The RecyclerView onto which dividers are being added
   */
  private void drawHorizontalDividers(Canvas canvas, RecyclerView parent)
  {
    int parentTop = parent.getPaddingTop();
    int parentBottom = parent.getHeight() - parent.getPaddingBottom();

    for (int i = 0; i < mNumColumns; i++)
    {
      View child = parent.getChildAt(i);
      RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

      int parentLeft = child.getRight() + params.rightMargin;
      int parentRight = parentLeft + mHorizontalDivider.getIntrinsicWidth();

      mHorizontalDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom);
      mHorizontalDivider.draw(canvas);
    }
  }

  /**
   * Adds vertical dividers to a RecyclerView with a GridLayoutManager or its
   * subclass.
   *
   * @param canvas The {@link Canvas} onto which dividers will be drawn
   * @param parent The RecyclerView onto which dividers are being added
   */
  private void drawVerticalDividers(Canvas canvas, RecyclerView parent)
  {
    int parentLeft = parent.getPaddingLeft();
    int parentRight = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    int numRows = (childCount + (mNumColumns - 1)) / mNumColumns;
    for (int i = 0; i < numRows - 1; i++)
    {
      View child = parent.getChildAt(i * mNumColumns);
      RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

      int parentTop = child.getBottom() + params.bottomMargin;
      int parentBottom = parentTop + mVerticalDivider.getIntrinsicHeight();

      mVerticalDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom);
      mVerticalDivider.draw(canvas);
    }
  }
}