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

TagLayoutManager.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: f4297ba2b953ae06817d7b2aff8b34934ec0f465 (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
package com.mapswithme.maps.widget.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * This LayoutManager designed only for use with RecyclerView.setNestedScrollingEnabled(false)
 * and recycle item must be wrap_content or fixed size
 */
public class TagLayoutManager extends RecyclerView.LayoutManager
{
  public TagLayoutManager()
  {
    setAutoMeasureEnabled(true);
  }

  @Override
  public RecyclerView.LayoutParams generateDefaultLayoutParams()
  {
    return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                                         RecyclerView.LayoutParams.WRAP_CONTENT);
  }

  @Override
  public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
  {
    detachAndScrapAttachedViews(recycler);

    int widthUsed = 0;
    int heightUsed = 0;
    int lineHeight = 0;
    int itemsCountOneLine = 0;
    for (int i = 0; i < getItemCount(); i++)
    {
      View child = recycler.getViewForPosition(i);
      addView(child);
      measureChildWithMargins(child, widthUsed, heightUsed);
      int width = getDecoratedMeasuredWidth(child);
      int height = getDecoratedMeasuredHeight(child);
      lineHeight = Math.max(lineHeight, height);
      if (widthUsed + width >= getWidth())
      {
        widthUsed = 0;
        if (itemsCountOneLine > 0)
        {
          itemsCountOneLine = -1;
          heightUsed += lineHeight;
          child.forceLayout();
          measureChildWithMargins(child, widthUsed, heightUsed);
          width = getDecoratedMeasuredWidth(child);
          height = getDecoratedMeasuredHeight(child);
        }
        lineHeight = 0;
      }
      layoutDecorated(child, widthUsed, heightUsed, widthUsed + width, heightUsed + height);
      widthUsed += width;
      itemsCountOneLine++;
    }
  }
}