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

TagItemDecoration.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: ddcbe254eb84259dc97d948e43460085b8195ab6 (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
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 TagLayoutManager or its
 * subclass.
 */
public class TagItemDecoration extends RecyclerView.ItemDecoration
{
  @NonNull
  private final Drawable mDivider;

  public TagItemDecoration(@NonNull Drawable divider)
  {
    mDivider = divider;
  }

  /**
   * Draws horizontal and 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)
  {
    if (state.isMeasuring())
      return;

    int parentRight = parent.getWidth() - parent.getPaddingRight();
    int parentLeft = parent.getPaddingLeft();
    int lastHeight = Integer.MIN_VALUE;

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++)
    {
      View child = parent.getChildAt(i);

      if (child.getTop() <= lastHeight)
      {
        mDivider.setBounds(child.getLeft() - mDivider.getIntrinsicWidth(),
                           child.getTop(),
                           child.getLeft(),
                           child.getBottom());
      }
      else
      {
        mDivider.setBounds(parentLeft,
                           child.getTop() - mDivider.getIntrinsicHeight(),
                           parentRight,
                           child.getTop());
      }
      mDivider.draw(canvas);
      lastHeight = child.getTop();
    }
  }

  /**
   * 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);

    outRect.left = mDivider.getIntrinsicWidth();
    outRect.top = mDivider.getIntrinsicHeight();
  }

  @NonNull
  protected Drawable getDivider()
  {
    return mDivider;
  }
}