OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.suggestions; | 5 package org.chromium.chrome.browser.suggestions; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
9 import android.text.TextUtils; | 9 import android.text.TextUtils; |
10 import android.util.AttributeSet; | 10 import android.util.AttributeSet; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.widget.FrameLayout; | 12 import android.widget.FrameLayout; |
13 | 13 |
14 import org.chromium.base.ApiCompatibilityUtils; | 14 import org.chromium.base.ApiCompatibilityUtils; |
15 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
16 import org.chromium.chrome.browser.util.MathUtils; | 16 import org.chromium.chrome.browser.util.MathUtils; |
17 | 17 |
18 /** | 18 /** |
19 * A layout that arranges tiles in a grid. | 19 * A layout that arranges tiles in a grid. |
20 */ | 20 */ |
21 public class TileGridLayout extends FrameLayout { | 21 public class TileGridLayout extends FrameLayout { |
22 private static final int MAX_COLUMNS = 4; | 22 private final int mVerticalSpacing; |
| 23 private final int mMinHorizontalSpacing; |
| 24 private final int mMaxHorizontalSpacing; |
| 25 private final int mMaxWidth; |
23 | 26 |
24 private int mVerticalSpacing; | 27 private int mMaxRows; |
| 28 private int mMaxColumns; |
25 private int mExtraVerticalSpacing; | 29 private int mExtraVerticalSpacing; |
26 private int mMinHorizontalSpacing; | |
27 private int mMaxHorizontalSpacing; | |
28 private int mMaxWidth; | |
29 private int mMaxRows; | |
30 | 30 |
31 /** | 31 /** |
| 32 * Constructor for inflating from XML. |
| 33 * |
32 * @param context The view context in which this item will be shown. | 34 * @param context The view context in which this item will be shown. |
33 * @param attrs The attributes of the XML tag that is inflating the view. | 35 * @param attrs The attributes of the XML tag that is inflating the view. |
34 */ | 36 */ |
35 public TileGridLayout(Context context, AttributeSet attrs) { | 37 public TileGridLayout(Context context, AttributeSet attrs) { |
36 super(context, attrs); | 38 super(context, attrs); |
37 | 39 |
38 Resources res = getResources(); | 40 Resources res = getResources(); |
39 mVerticalSpacing = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_
vertical_spacing); | 41 mVerticalSpacing = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_
vertical_spacing); |
40 mMinHorizontalSpacing = | 42 mMinHorizontalSpacing = |
41 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_min_horizon
tal_spacing); | 43 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_min_horizon
tal_spacing); |
42 mMaxHorizontalSpacing = | 44 mMaxHorizontalSpacing = |
43 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_horizon
tal_spacing); | 45 res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_horizon
tal_spacing); |
44 mMaxWidth = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_wid
th); | 46 mMaxWidth = res.getDimensionPixelOffset(R.dimen.tile_grid_layout_max_wid
th); |
45 } | 47 } |
46 | 48 |
47 /** | 49 /** |
48 * Sets the maximum number of rows to display. Any items that don't fit with
in these rows will | 50 * Sets the maximum number of rows to display. Any items that don't fit will
be hidden. |
49 * be hidden. | |
50 */ | 51 */ |
51 public void setMaxRows(int rows) { | 52 public void setMaxRows(int rows) { |
52 mMaxRows = rows; | 53 mMaxRows = rows; |
53 } | 54 } |
54 | 55 |
55 /** | 56 /** |
| 57 * Sets the maximum number of columns to display. Any items that don't fit w
ill be hidden. |
| 58 */ |
| 59 public void setMaxColumns(int columns) { |
| 60 mMaxColumns = columns; |
| 61 } |
| 62 |
| 63 /** |
56 * Sets the extra vertical spacing that must be used. It will be distributed
evenly above each | 64 * Sets the extra vertical spacing that must be used. It will be distributed
evenly above each |
57 * row. | 65 * row. |
58 */ | 66 */ |
59 public void setExtraVerticalSpacing(int spacing) { | 67 public void setExtraVerticalSpacing(int spacing) { |
60 if (mExtraVerticalSpacing == spacing) { | 68 if (mExtraVerticalSpacing == spacing) { |
61 return; | 69 return; |
62 } | 70 } |
63 mExtraVerticalSpacing = spacing; | 71 mExtraVerticalSpacing = spacing; |
64 | 72 |
65 // Clear the measure cache for this view and make sure it will be remeas
ured. | 73 // Clear the measure cache for this view and make sure it will be remeas
ured. |
(...skipping 29 matching lines...) Expand all Loading... |
95 measureChild(getChildAt(i), MeasureSpec.UNSPECIFIED, MeasureSpec.UNS
PECIFIED); | 103 measureChild(getChildAt(i), MeasureSpec.UNSPECIFIED, MeasureSpec.UNS
PECIFIED); |
96 } | 104 } |
97 | 105 |
98 // Determine the number of columns that will fit. | 106 // Determine the number of columns that will fit. |
99 int gridWidth = totalWidth - ApiCompatibilityUtils.getPaddingStart(this) | 107 int gridWidth = totalWidth - ApiCompatibilityUtils.getPaddingStart(this) |
100 - ApiCompatibilityUtils.getPaddingEnd(this); | 108 - ApiCompatibilityUtils.getPaddingEnd(this); |
101 int childHeight = getChildAt(0).getMeasuredHeight(); | 109 int childHeight = getChildAt(0).getMeasuredHeight(); |
102 int childWidth = getChildAt(0).getMeasuredWidth(); | 110 int childWidth = getChildAt(0).getMeasuredWidth(); |
103 int numColumns = MathUtils.clamp( | 111 int numColumns = MathUtils.clamp( |
104 (gridWidth + mMinHorizontalSpacing) / (childWidth + mMinHorizont
alSpacing), 1, | 112 (gridWidth + mMinHorizontalSpacing) / (childWidth + mMinHorizont
alSpacing), 1, |
105 MAX_COLUMNS); | 113 mMaxColumns); |
106 | 114 |
107 // Ensure column spacing isn't greater than mMaxHorizontalSpacing. | 115 // Ensure column spacing isn't greater than mMaxHorizontalSpacing. |
108 int gridWidthMinusColumns = Math.max(0, gridWidth - numColumns * childWi
dth); | 116 int gridWidthMinusColumns = Math.max(0, gridWidth - numColumns * childWi
dth); |
109 int gridSidePadding = gridWidthMinusColumns - mMaxHorizontalSpacing * (n
umColumns - 1); | 117 int gridSidePadding = gridWidthMinusColumns - mMaxHorizontalSpacing * (n
umColumns - 1); |
110 | 118 |
111 int gridStart = 0; | 119 int gridStart = 0; |
112 float horizontalSpacing; | 120 float horizontalSpacing; |
113 if (gridSidePadding > 0) { | 121 if (gridSidePadding > 0) { |
114 horizontalSpacing = mMaxHorizontalSpacing; | 122 horizontalSpacing = mMaxHorizontalSpacing; |
115 gridStart = gridSidePadding / 2; | 123 gridStart = gridSidePadding / 2; |
(...skipping 26 matching lines...) Expand all Loading... |
142 for (int i = visibleChildCount; i < childCount; i++) { | 150 for (int i = visibleChildCount; i < childCount; i++) { |
143 getChildAt(i).setVisibility(View.GONE); | 151 getChildAt(i).setVisibility(View.GONE); |
144 } | 152 } |
145 | 153 |
146 int totalHeight = paddingTop + getPaddingBottom() + numRows * childHeigh
t | 154 int totalHeight = paddingTop + getPaddingBottom() + numRows * childHeigh
t |
147 + (numRows - 1) * mVerticalSpacing + mExtraVerticalSpacing; | 155 + (numRows - 1) * mVerticalSpacing + mExtraVerticalSpacing; |
148 | 156 |
149 setMeasuredDimension(totalWidth, resolveSize(totalHeight, heightMeasureS
pec)); | 157 setMeasuredDimension(totalWidth, resolveSize(totalHeight, heightMeasureS
pec)); |
150 } | 158 } |
151 } | 159 } |
OLD | NEW |