Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Color; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 24 | import android.graphics.Insets; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 25 | import android.graphics.Paint; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 28 | import android.util.LogPrinter; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 29 | import android.util.Pair; |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 30 | import android.util.Printer; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 31 | import android.view.Gravity; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 34 | import android.view.accessibility.AccessibilityEvent; |
| 35 | import android.view.accessibility.AccessibilityNodeInfo; |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 36 | import android.widget.RemoteViews.RemoteView; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 37 | import com.android.internal.R; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 38 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 41 | import java.lang.reflect.Array; |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.Arrays; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 44 | import java.util.HashMap; |
| 45 | import java.util.List; |
| 46 | import java.util.Map; |
| 47 | |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 48 | import static android.view.Gravity.*; |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 49 | import static android.view.View.MeasureSpec.EXACTLY; |
| 50 | import static android.view.View.MeasureSpec.makeMeasureSpec; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 51 | import static java.lang.Math.max; |
| 52 | import static java.lang.Math.min; |
| 53 | |
| 54 | /** |
| 55 | * A layout that places its children in a rectangular <em>grid</em>. |
| 56 | * <p> |
| 57 | * The grid is composed of a set of infinitely thin lines that separate the |
| 58 | * viewing area into <em>cells</em>. Throughout the API, grid lines are referenced |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 59 | * by grid <em>indices</em>. A grid with {@code N} columns |
| 60 | * has {@code N + 1} grid indices that run from {@code 0} |
| 61 | * through {@code N} inclusive. Regardless of how GridLayout is |
| 62 | * configured, grid index {@code 0} is fixed to the leading edge of the |
| 63 | * container and grid index {@code N} is fixed to its trailing edge |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 64 | * (after padding is taken into account). |
| 65 | * |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 66 | * <h4>Row and Column Specs</h4> |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 67 | * |
| 68 | * Children occupy one or more contiguous cells, as defined |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 69 | * by their {@link GridLayout.LayoutParams#rowSpec rowSpec} and |
| 70 | * {@link GridLayout.LayoutParams#columnSpec columnSpec} layout parameters. |
| 71 | * Each spec defines the set of rows or columns that are to be |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 72 | * occupied; and how children should be aligned within the resulting group of cells. |
| 73 | * Although cells do not normally overlap in a GridLayout, GridLayout does |
| 74 | * not prevent children being defined to occupy the same cell or group of cells. |
| 75 | * In this case however, there is no guarantee that children will not themselves |
| 76 | * overlap after the layout operation completes. |
| 77 | * |
| 78 | * <h4>Default Cell Assignment</h4> |
| 79 | * |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 80 | * If a child does not specify the row and column indices of the cell it |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 81 | * wishes to occupy, GridLayout assigns cell locations automatically using its: |
| 82 | * {@link GridLayout#setOrientation(int) orientation}, |
| 83 | * {@link GridLayout#setRowCount(int) rowCount} and |
| 84 | * {@link GridLayout#setColumnCount(int) columnCount} properties. |
| 85 | * |
| 86 | * <h4>Space</h4> |
| 87 | * |
| 88 | * Space between children may be specified either by using instances of the |
| 89 | * dedicated {@link Space} view or by setting the |
| 90 | * |
| 91 | * {@link ViewGroup.MarginLayoutParams#leftMargin leftMargin}, |
| 92 | * {@link ViewGroup.MarginLayoutParams#topMargin topMargin}, |
| 93 | * {@link ViewGroup.MarginLayoutParams#rightMargin rightMargin} and |
| 94 | * {@link ViewGroup.MarginLayoutParams#bottomMargin bottomMargin} |
| 95 | * |
| 96 | * layout parameters. When the |
| 97 | * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} |
| 98 | * property is set, default margins around children are automatically |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 99 | * allocated based on the prevailing UI style guide for the platform. |
| 100 | * Each of the margins so defined may be independently overridden by an assignment |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 101 | * to the appropriate layout parameter. |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 102 | * Default values will generally produce a reasonable spacing between components |
| 103 | * but values may change between different releases of the platform. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 104 | * |
| 105 | * <h4>Excess Space Distribution</h4> |
| 106 | * |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 107 | * GridLayout's distribution of excess space is based on <em>priority</em> |
| 108 | * rather than <em>weight</em>. |
| 109 | * <p> |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 110 | * A child's ability to stretch is inferred from the alignment properties of |
| 111 | * its row and column groups (which are typically set by setting the |
| 112 | * {@link LayoutParams#setGravity(int) gravity} property of the child's layout parameters). |
| 113 | * If alignment was defined along a given axis then the component |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 114 | * is taken as <em>flexible</em> in that direction. If no alignment was set, |
| 115 | * the component is instead assumed to be <em>inflexible</em>. |
| 116 | * <p> |
| 117 | * Multiple components in the same row or column group are |
| 118 | * considered to act in <em>parallel</em>. Such a |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 119 | * group is flexible only if <em>all</em> of the components |
| 120 | * within it are flexible. Row and column groups that sit either side of a common boundary |
| 121 | * are instead considered to act in <em>series</em>. The composite group made of these two |
| 122 | * elements is flexible if <em>one</em> of its elements is flexible. |
| 123 | * <p> |
| 124 | * To make a column stretch, make sure all of the components inside it define a |
| 125 | * gravity. To prevent a column from stretching, ensure that one of the components |
| 126 | * in the column does not define a gravity. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 127 | * <p> |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 128 | * When the principle of flexibility does not provide complete disambiguation, |
| 129 | * GridLayout's algorithms favour rows and columns that are closer to its <em>right</em> |
| 130 | * and <em>bottom</em> edges. |
| 131 | * |
Philip Milne | a841644 | 2013-02-25 10:49:39 -0800 | [diff] [blame] | 132 | * <h4>Interpretation of GONE</h4> |
| 133 | * |
| 134 | * For layout purposes, GridLayout treats views whose visibility status is |
| 135 | * {@link View#GONE GONE}, as having zero width and height. This is subtly different from |
| 136 | * the policy of ignoring views that are marked as GONE outright. If, for example, a gone-marked |
| 137 | * view was alone in a column, that column would itself collapse to zero width if and only if |
| 138 | * no gravity was defined on the view. If gravity was defined, then the gone-marked |
| 139 | * view has no effect on the layout and the container should be laid out as if the view |
| 140 | * had never been added to it. |
| 141 | * These statements apply equally to rows as well as columns, and to groups of rows or columns. |
| 142 | * |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 143 | * <h5>Limitations</h5> |
| 144 | * |
| 145 | * GridLayout does not provide support for the principle of <em>weight</em>, as defined in |
| 146 | * {@link LinearLayout.LayoutParams#weight}. In general, it is not therefore possible |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 147 | * to configure a GridLayout to distribute excess space between multiple components. |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 148 | * <p> |
| 149 | * Some common use-cases may nevertheless be accommodated as follows. |
| 150 | * To place equal amounts of space around a component in a cell group; |
| 151 | * use {@link #CENTER} alignment (or {@link LayoutParams#setGravity(int) gravity}). |
| 152 | * For complete control over excess space distribution in a row or column; |
| 153 | * use a {@link LinearLayout} subview to hold the components in the associated cell group. |
| 154 | * When using either of these techniques, bear in mind that cell groups may be defined to overlap. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 155 | * <p> |
| 156 | * See {@link GridLayout.LayoutParams} for a full description of the |
| 157 | * layout parameters used by GridLayout. |
| 158 | * |
| 159 | * @attr ref android.R.styleable#GridLayout_orientation |
| 160 | * @attr ref android.R.styleable#GridLayout_rowCount |
| 161 | * @attr ref android.R.styleable#GridLayout_columnCount |
| 162 | * @attr ref android.R.styleable#GridLayout_useDefaultMargins |
| 163 | * @attr ref android.R.styleable#GridLayout_rowOrderPreserved |
| 164 | * @attr ref android.R.styleable#GridLayout_columnOrderPreserved |
| 165 | */ |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 166 | @RemoteView |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 167 | public class GridLayout extends ViewGroup { |
| 168 | |
| 169 | // Public constants |
| 170 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 171 | /** @hide */ |
| 172 | @IntDef({HORIZONTAL, VERTICAL}) |
| 173 | @Retention(RetentionPolicy.SOURCE) |
| 174 | public @interface Orientation {} |
| 175 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 176 | /** |
| 177 | * The horizontal orientation. |
| 178 | */ |
| 179 | public static final int HORIZONTAL = LinearLayout.HORIZONTAL; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 180 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 181 | /** |
| 182 | * The vertical orientation. |
| 183 | */ |
| 184 | public static final int VERTICAL = LinearLayout.VERTICAL; |
| 185 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 186 | /** |
| 187 | * The constant used to indicate that a value is undefined. |
| 188 | * Fields can use this value to indicate that their values |
| 189 | * have not yet been set. Similarly, methods can return this value |
| 190 | * to indicate that there is no suitable value that the implementation |
| 191 | * can return. |
| 192 | * The value used for the constant (currently {@link Integer#MIN_VALUE}) is |
| 193 | * intended to avoid confusion between valid values whose sign may not be known. |
| 194 | */ |
| 195 | public static final int UNDEFINED = Integer.MIN_VALUE; |
| 196 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 197 | /** @hide */ |
| 198 | @IntDef({ALIGN_BOUNDS, ALIGN_MARGINS}) |
| 199 | @Retention(RetentionPolicy.SOURCE) |
| 200 | public @interface AlignmentMode {} |
| 201 | |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 202 | /** |
| 203 | * This constant is an {@link #setAlignmentMode(int) alignmentMode}. |
| 204 | * When the {@code alignmentMode} is set to {@link #ALIGN_BOUNDS}, alignment |
| 205 | * is made between the edges of each component's raw |
| 206 | * view boundary: i.e. the area delimited by the component's: |
| 207 | * {@link android.view.View#getTop() top}, |
| 208 | * {@link android.view.View#getLeft() left}, |
| 209 | * {@link android.view.View#getBottom() bottom} and |
| 210 | * {@link android.view.View#getRight() right} properties. |
| 211 | * <p> |
| 212 | * For example, when {@code GridLayout} is in {@link #ALIGN_BOUNDS} mode, |
| 213 | * children that belong to a row group that uses {@link #TOP} alignment will |
| 214 | * all return the same value when their {@link android.view.View#getTop()} |
| 215 | * method is called. |
| 216 | * |
| 217 | * @see #setAlignmentMode(int) |
| 218 | */ |
| 219 | public static final int ALIGN_BOUNDS = 0; |
| 220 | |
| 221 | /** |
| 222 | * This constant is an {@link #setAlignmentMode(int) alignmentMode}. |
| 223 | * When the {@code alignmentMode} is set to {@link #ALIGN_MARGINS}, |
| 224 | * the bounds of each view are extended outwards, according |
| 225 | * to their margins, before the edges of the resulting rectangle are aligned. |
| 226 | * <p> |
| 227 | * For example, when {@code GridLayout} is in {@link #ALIGN_MARGINS} mode, |
| 228 | * the quantity {@code top - layoutParams.topMargin} is the same for all children that |
| 229 | * belong to a row group that uses {@link #TOP} alignment. |
| 230 | * |
| 231 | * @see #setAlignmentMode(int) |
| 232 | */ |
| 233 | public static final int ALIGN_MARGINS = 1; |
| 234 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 235 | // Misc constants |
| 236 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 237 | static final int MAX_SIZE = 100000; |
| 238 | static final int DEFAULT_CONTAINER_MARGIN = 0; |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 239 | static final int UNINITIALIZED_HASH = 0; |
Philip Milne | a235362 | 2013-03-05 12:19:15 -0800 | [diff] [blame] | 240 | static final Printer LOG_PRINTER = new LogPrinter(Log.DEBUG, GridLayout.class.getName()); |
| 241 | static final Printer NO_PRINTER = new Printer() { |
| 242 | @Override |
| 243 | public void println(String x) { |
| 244 | } |
| 245 | }; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 246 | |
| 247 | // Defaults |
| 248 | |
| 249 | private static final int DEFAULT_ORIENTATION = HORIZONTAL; |
| 250 | private static final int DEFAULT_COUNT = UNDEFINED; |
| 251 | private static final boolean DEFAULT_USE_DEFAULT_MARGINS = false; |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 252 | private static final boolean DEFAULT_ORDER_PRESERVED = true; |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 253 | private static final int DEFAULT_ALIGNMENT_MODE = ALIGN_MARGINS; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 254 | |
| 255 | // TypedArray indices |
| 256 | |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 257 | private static final int ORIENTATION = R.styleable.GridLayout_orientation; |
| 258 | private static final int ROW_COUNT = R.styleable.GridLayout_rowCount; |
| 259 | private static final int COLUMN_COUNT = R.styleable.GridLayout_columnCount; |
| 260 | private static final int USE_DEFAULT_MARGINS = R.styleable.GridLayout_useDefaultMargins; |
| 261 | private static final int ALIGNMENT_MODE = R.styleable.GridLayout_alignmentMode; |
| 262 | private static final int ROW_ORDER_PRESERVED = R.styleable.GridLayout_rowOrderPreserved; |
| 263 | private static final int COLUMN_ORDER_PRESERVED = R.styleable.GridLayout_columnOrderPreserved; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 264 | |
| 265 | // Instance variables |
| 266 | |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 267 | final Axis mHorizontalAxis = new Axis(true); |
| 268 | final Axis mVerticalAxis = new Axis(false); |
| 269 | int mOrientation = DEFAULT_ORIENTATION; |
| 270 | boolean mUseDefaultMargins = DEFAULT_USE_DEFAULT_MARGINS; |
| 271 | int mAlignmentMode = DEFAULT_ALIGNMENT_MODE; |
| 272 | int mDefaultGap; |
| 273 | int mLastLayoutParamsHashCode = UNINITIALIZED_HASH; |
| 274 | Printer mPrinter = LOG_PRINTER; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 275 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 276 | // Constructors |
| 277 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 278 | public GridLayout(Context context) { |
| 279 | this(context, null); |
| 280 | } |
| 281 | |
| 282 | public GridLayout(Context context, AttributeSet attrs) { |
| 283 | this(context, attrs, 0); |
| 284 | } |
| 285 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 286 | public GridLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 287 | this(context, attrs, defStyleAttr, 0); |
| 288 | } |
| 289 | |
| 290 | public GridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 291 | super(context, attrs, defStyleAttr, defStyleRes); |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 292 | mDefaultGap = context.getResources().getDimensionPixelOffset(R.dimen.default_gap); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 293 | final TypedArray a = context.obtainStyledAttributes( |
| 294 | attrs, R.styleable.GridLayout, defStyleAttr, defStyleRes); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 295 | try { |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 296 | setRowCount(a.getInt(ROW_COUNT, DEFAULT_COUNT)); |
| 297 | setColumnCount(a.getInt(COLUMN_COUNT, DEFAULT_COUNT)); |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 298 | setOrientation(a.getInt(ORIENTATION, DEFAULT_ORIENTATION)); |
| 299 | setUseDefaultMargins(a.getBoolean(USE_DEFAULT_MARGINS, DEFAULT_USE_DEFAULT_MARGINS)); |
| 300 | setAlignmentMode(a.getInt(ALIGNMENT_MODE, DEFAULT_ALIGNMENT_MODE)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 301 | setRowOrderPreserved(a.getBoolean(ROW_ORDER_PRESERVED, DEFAULT_ORDER_PRESERVED)); |
| 302 | setColumnOrderPreserved(a.getBoolean(COLUMN_ORDER_PRESERVED, DEFAULT_ORDER_PRESERVED)); |
| 303 | } finally { |
| 304 | a.recycle(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Implementation |
| 309 | |
| 310 | /** |
| 311 | * Returns the current orientation. |
| 312 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 313 | * @return either {@link #HORIZONTAL} or {@link #VERTICAL} |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 314 | * |
| 315 | * @see #setOrientation(int) |
| 316 | * |
| 317 | * @attr ref android.R.styleable#GridLayout_orientation |
| 318 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 319 | @Orientation |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 320 | public int getOrientation() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 321 | return mOrientation; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
Philip Milne | b65408f | 2012-05-21 10:44:46 -0700 | [diff] [blame] | 325 | * |
| 326 | * GridLayout uses the orientation property for two purposes: |
| 327 | * <ul> |
| 328 | * <li> |
| 329 | * To control the 'direction' in which default row/column indices are generated |
| 330 | * when they are not specified in a component's layout parameters. |
| 331 | * </li> |
| 332 | * <li> |
| 333 | * To control which axis should be processed first during the layout operation: |
| 334 | * when orientation is {@link #HORIZONTAL} the horizontal axis is laid out first. |
| 335 | * </li> |
| 336 | * </ul> |
| 337 | * |
| 338 | * The order in which axes are laid out is important if, for example, the height of |
| 339 | * one of GridLayout's children is dependent on its width - and its width is, in turn, |
| 340 | * dependent on the widths of other components. |
| 341 | * <p> |
| 342 | * If your layout contains a {@link TextView} (or derivative: |
| 343 | * {@code Button}, {@code EditText}, {@code CheckBox}, etc.) which is |
| 344 | * in multi-line mode (the default) it is normally best to leave GridLayout's |
| 345 | * orientation as {@code HORIZONTAL} - because {@code TextView} is capable of |
| 346 | * deriving its height for a given width, but not the other way around. |
| 347 | * <p> |
| 348 | * Other than the effects above, orientation does not affect the actual layout operation of |
| 349 | * GridLayout, so it's fine to leave GridLayout in {@code HORIZONTAL} mode even if |
| 350 | * the height of the intended layout greatly exceeds its width. |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 351 | * <p> |
| 352 | * The default value of this property is {@link #HORIZONTAL}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 353 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 354 | * @param orientation either {@link #HORIZONTAL} or {@link #VERTICAL} |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 355 | * |
| 356 | * @see #getOrientation() |
| 357 | * |
| 358 | * @attr ref android.R.styleable#GridLayout_orientation |
| 359 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 360 | public void setOrientation(@Orientation int orientation) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 361 | if (this.mOrientation != orientation) { |
| 362 | this.mOrientation = orientation; |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 363 | invalidateStructure(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 364 | requestLayout(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Returns the current number of rows. This is either the last value that was set |
| 370 | * with {@link #setRowCount(int)} or, if no such value was set, the maximum |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 371 | * value of each the upper bounds defined in {@link LayoutParams#rowSpec}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 372 | * |
| 373 | * @return the current number of rows |
| 374 | * |
| 375 | * @see #setRowCount(int) |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 376 | * @see LayoutParams#rowSpec |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 377 | * |
| 378 | * @attr ref android.R.styleable#GridLayout_rowCount |
| 379 | */ |
| 380 | public int getRowCount() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 381 | return mVerticalAxis.getCount(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 385 | * RowCount is used only to generate default row/column indices when |
| 386 | * they are not specified by a component's layout parameters. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 387 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 388 | * @param rowCount the number of rows |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 389 | * |
| 390 | * @see #getRowCount() |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 391 | * @see LayoutParams#rowSpec |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 392 | * |
| 393 | * @attr ref android.R.styleable#GridLayout_rowCount |
| 394 | */ |
| 395 | public void setRowCount(int rowCount) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 396 | mVerticalAxis.setCount(rowCount); |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 397 | invalidateStructure(); |
| 398 | requestLayout(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Returns the current number of columns. This is either the last value that was set |
| 403 | * with {@link #setColumnCount(int)} or, if no such value was set, the maximum |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 404 | * value of each the upper bounds defined in {@link LayoutParams#columnSpec}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 405 | * |
| 406 | * @return the current number of columns |
| 407 | * |
| 408 | * @see #setColumnCount(int) |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 409 | * @see LayoutParams#columnSpec |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 410 | * |
| 411 | * @attr ref android.R.styleable#GridLayout_columnCount |
| 412 | */ |
| 413 | public int getColumnCount() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 414 | return mHorizontalAxis.getCount(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /** |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 418 | * ColumnCount is used only to generate default column/column indices when |
| 419 | * they are not specified by a component's layout parameters. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 420 | * |
| 421 | * @param columnCount the number of columns. |
| 422 | * |
| 423 | * @see #getColumnCount() |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 424 | * @see LayoutParams#columnSpec |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 425 | * |
| 426 | * @attr ref android.R.styleable#GridLayout_columnCount |
| 427 | */ |
| 428 | public void setColumnCount(int columnCount) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 429 | mHorizontalAxis.setCount(columnCount); |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 430 | invalidateStructure(); |
| 431 | requestLayout(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Returns whether or not this GridLayout will allocate default margins when no |
| 436 | * corresponding layout parameters are defined. |
| 437 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 438 | * @return {@code true} if default margins should be allocated |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 439 | * |
| 440 | * @see #setUseDefaultMargins(boolean) |
| 441 | * |
| 442 | * @attr ref android.R.styleable#GridLayout_useDefaultMargins |
| 443 | */ |
| 444 | public boolean getUseDefaultMargins() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 445 | return mUseDefaultMargins; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 449 | * When {@code true}, GridLayout allocates default margins around children |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 450 | * based on the child's visual characteristics. Each of the |
| 451 | * margins so defined may be independently overridden by an assignment |
| 452 | * to the appropriate layout parameter. |
| 453 | * <p> |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 454 | * When {@code false}, the default value of all margins is zero. |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 455 | * <p> |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 456 | * When setting to {@code true}, consider setting the value of the |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 457 | * {@link #setAlignmentMode(int) alignmentMode} |
| 458 | * property to {@link #ALIGN_BOUNDS}. |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 459 | * <p> |
| 460 | * The default value of this property is {@code false}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 461 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 462 | * @param useDefaultMargins use {@code true} to make GridLayout allocate default margins |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 463 | * |
| 464 | * @see #getUseDefaultMargins() |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 465 | * @see #setAlignmentMode(int) |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 466 | * |
| 467 | * @see MarginLayoutParams#leftMargin |
| 468 | * @see MarginLayoutParams#topMargin |
| 469 | * @see MarginLayoutParams#rightMargin |
| 470 | * @see MarginLayoutParams#bottomMargin |
| 471 | * |
| 472 | * @attr ref android.R.styleable#GridLayout_useDefaultMargins |
| 473 | */ |
| 474 | public void setUseDefaultMargins(boolean useDefaultMargins) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 475 | this.mUseDefaultMargins = useDefaultMargins; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 476 | requestLayout(); |
| 477 | } |
| 478 | |
| 479 | /** |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 480 | * Returns the alignment mode. |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 481 | * |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 482 | * @return the alignment mode; either {@link #ALIGN_BOUNDS} or {@link #ALIGN_MARGINS} |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 483 | * |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 484 | * @see #ALIGN_BOUNDS |
| 485 | * @see #ALIGN_MARGINS |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 486 | * |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 487 | * @see #setAlignmentMode(int) |
| 488 | * |
| 489 | * @attr ref android.R.styleable#GridLayout_alignmentMode |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 490 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 491 | @AlignmentMode |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 492 | public int getAlignmentMode() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 493 | return mAlignmentMode; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /** |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 497 | * Sets the alignment mode to be used for all of the alignments between the |
| 498 | * children of this container. |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 499 | * <p> |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 500 | * The default value of this property is {@link #ALIGN_MARGINS}. |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 501 | * |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 502 | * @param alignmentMode either {@link #ALIGN_BOUNDS} or {@link #ALIGN_MARGINS} |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 503 | * |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 504 | * @see #ALIGN_BOUNDS |
| 505 | * @see #ALIGN_MARGINS |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 506 | * |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 507 | * @see #getAlignmentMode() |
| 508 | * |
| 509 | * @attr ref android.R.styleable#GridLayout_alignmentMode |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 510 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 511 | public void setAlignmentMode(@AlignmentMode int alignmentMode) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 512 | this.mAlignmentMode = alignmentMode; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 513 | requestLayout(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Returns whether or not row boundaries are ordered by their grid indices. |
| 518 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 519 | * @return {@code true} if row boundaries must appear in the order of their indices, |
| 520 | * {@code false} otherwise |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 521 | * |
| 522 | * @see #setRowOrderPreserved(boolean) |
| 523 | * |
| 524 | * @attr ref android.R.styleable#GridLayout_rowOrderPreserved |
| 525 | */ |
| 526 | public boolean isRowOrderPreserved() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 527 | return mVerticalAxis.isOrderPreserved(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 531 | * When this property is {@code true}, GridLayout is forced to place the row boundaries |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 532 | * so that their associated grid indices are in ascending order in the view. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 533 | * <p> |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 534 | * When this property is {@code false} GridLayout is at liberty to place the vertical row |
| 535 | * boundaries in whatever order best fits the given constraints. |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 536 | * <p> |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 537 | * The default value of this property is {@code true}. |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 538 | |
| 539 | * @param rowOrderPreserved {@code true} to force GridLayout to respect the order |
| 540 | * of row boundaries |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 541 | * |
| 542 | * @see #isRowOrderPreserved() |
| 543 | * |
| 544 | * @attr ref android.R.styleable#GridLayout_rowOrderPreserved |
| 545 | */ |
| 546 | public void setRowOrderPreserved(boolean rowOrderPreserved) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 547 | mVerticalAxis.setOrderPreserved(rowOrderPreserved); |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 548 | invalidateStructure(); |
| 549 | requestLayout(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Returns whether or not column boundaries are ordered by their grid indices. |
| 554 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 555 | * @return {@code true} if column boundaries must appear in the order of their indices, |
| 556 | * {@code false} otherwise |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 557 | * |
| 558 | * @see #setColumnOrderPreserved(boolean) |
| 559 | * |
| 560 | * @attr ref android.R.styleable#GridLayout_columnOrderPreserved |
| 561 | */ |
| 562 | public boolean isColumnOrderPreserved() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 563 | return mHorizontalAxis.isOrderPreserved(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 567 | * When this property is {@code true}, GridLayout is forced to place the column boundaries |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 568 | * so that their associated grid indices are in ascending order in the view. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 569 | * <p> |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 570 | * When this property is {@code false} GridLayout is at liberty to place the horizontal column |
| 571 | * boundaries in whatever order best fits the given constraints. |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 572 | * <p> |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 573 | * The default value of this property is {@code true}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 574 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 575 | * @param columnOrderPreserved use {@code true} to force GridLayout to respect the order |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 576 | * of column boundaries. |
| 577 | * |
| 578 | * @see #isColumnOrderPreserved() |
| 579 | * |
| 580 | * @attr ref android.R.styleable#GridLayout_columnOrderPreserved |
| 581 | */ |
| 582 | public void setColumnOrderPreserved(boolean columnOrderPreserved) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 583 | mHorizontalAxis.setOrderPreserved(columnOrderPreserved); |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 584 | invalidateStructure(); |
| 585 | requestLayout(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 588 | /** |
| 589 | * Return the printer that will log diagnostics from this layout. |
| 590 | * |
| 591 | * @see #setPrinter(android.util.Printer) |
| 592 | * |
| 593 | * @return the printer associated with this view |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 594 | * |
| 595 | * @hide |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 596 | */ |
| 597 | public Printer getPrinter() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 598 | return mPrinter; |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Set the printer that will log diagnostics from this layout. |
| 603 | * The default value is created by {@link android.util.LogPrinter}. |
| 604 | * |
| 605 | * @param printer the printer associated with this layout |
| 606 | * |
| 607 | * @see #getPrinter() |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 608 | * |
| 609 | * @hide |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 610 | */ |
| 611 | public void setPrinter(Printer printer) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 612 | this.mPrinter = (printer == null) ? NO_PRINTER : printer; |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 615 | // Static utility methods |
| 616 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 617 | static int max2(int[] a, int valueIfEmpty) { |
Philip Milne | 51f17d5 | 2011-06-13 10:44:49 -0700 | [diff] [blame] | 618 | int result = valueIfEmpty; |
| 619 | for (int i = 0, N = a.length; i < N; i++) { |
| 620 | result = Math.max(result, a[i]); |
| 621 | } |
| 622 | return result; |
| 623 | } |
| 624 | |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 625 | @SuppressWarnings("unchecked") |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 626 | static <T> T[] append(T[] a, T[] b) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 627 | T[] result = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length); |
| 628 | System.arraycopy(a, 0, result, 0, a.length); |
| 629 | System.arraycopy(b, 0, result, a.length, b.length); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 630 | return result; |
| 631 | } |
| 632 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 633 | static Alignment getAlignment(int gravity, boolean horizontal) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 634 | int mask = horizontal ? HORIZONTAL_GRAVITY_MASK : VERTICAL_GRAVITY_MASK; |
| 635 | int shift = horizontal ? AXIS_X_SHIFT : AXIS_Y_SHIFT; |
| 636 | int flags = (gravity & mask) >> shift; |
| 637 | switch (flags) { |
| 638 | case (AXIS_SPECIFIED | AXIS_PULL_BEFORE): |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 639 | return horizontal ? LEFT : TOP; |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 640 | case (AXIS_SPECIFIED | AXIS_PULL_AFTER): |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 641 | return horizontal ? RIGHT : BOTTOM; |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 642 | case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | AXIS_PULL_AFTER): |
| 643 | return FILL; |
| 644 | case AXIS_SPECIFIED: |
| 645 | return CENTER; |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 646 | case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | RELATIVE_LAYOUT_DIRECTION): |
| 647 | return START; |
| 648 | case (AXIS_SPECIFIED | AXIS_PULL_AFTER | RELATIVE_LAYOUT_DIRECTION): |
| 649 | return END; |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 650 | default: |
| 651 | return UNDEFINED_ALIGNMENT; |
| 652 | } |
| 653 | } |
| 654 | |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 655 | /** @noinspection UnusedParameters*/ |
Philip Milne | 1fd1637 | 2011-06-21 14:57:47 -0700 | [diff] [blame] | 656 | private int getDefaultMargin(View c, boolean horizontal, boolean leading) { |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 657 | if (c.getClass() == Space.class) { |
| 658 | return 0; |
| 659 | } |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 660 | return mDefaultGap / 2; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Philip Milne | 1fd1637 | 2011-06-21 14:57:47 -0700 | [diff] [blame] | 663 | private int getDefaultMargin(View c, boolean isAtEdge, boolean horizontal, boolean leading) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 664 | return /*isAtEdge ? DEFAULT_CONTAINER_MARGIN :*/ getDefaultMargin(c, horizontal, leading); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 665 | } |
| 666 | |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 667 | private int getDefaultMargin(View c, LayoutParams p, boolean horizontal, boolean leading) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 668 | if (!mUseDefaultMargins) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 669 | return 0; |
| 670 | } |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 671 | Spec spec = horizontal ? p.columnSpec : p.rowSpec; |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 672 | Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 673 | Interval span = spec.span; |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 674 | boolean leading1 = (horizontal && isLayoutRtl()) ? !leading : leading; |
| 675 | boolean isAtEdge = leading1 ? (span.min == 0) : (span.max == axis.getCount()); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 676 | |
Philip Milne | 1fd1637 | 2011-06-21 14:57:47 -0700 | [diff] [blame] | 677 | return getDefaultMargin(c, isAtEdge, horizontal, leading); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 680 | int getMargin1(View view, boolean horizontal, boolean leading) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 681 | LayoutParams lp = getLayoutParams(view); |
| 682 | int margin = horizontal ? |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 683 | (leading ? lp.leftMargin : lp.rightMargin) : |
| 684 | (leading ? lp.topMargin : lp.bottomMargin); |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 685 | return margin == UNDEFINED ? getDefaultMargin(view, lp, horizontal, leading) : margin; |
Philip Milne | 1fd1637 | 2011-06-21 14:57:47 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 688 | private int getMargin(View view, boolean horizontal, boolean leading) { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 689 | if (mAlignmentMode == ALIGN_MARGINS) { |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 690 | return getMargin1(view, horizontal, leading); |
| 691 | } else { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 692 | Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 693 | int[] margins = leading ? axis.getLeadingMargins() : axis.getTrailingMargins(); |
| 694 | LayoutParams lp = getLayoutParams(view); |
| 695 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 696 | int index = leading ? spec.span.min : spec.span.max; |
| 697 | return margins[index]; |
| 698 | } |
| 699 | } |
| 700 | |
Philip Milne | 1fd1637 | 2011-06-21 14:57:47 -0700 | [diff] [blame] | 701 | private int getTotalMargin(View child, boolean horizontal) { |
| 702 | return getMargin(child, horizontal, true) + getMargin(child, horizontal, false); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 705 | private static boolean fits(int[] a, int value, int start, int end) { |
| 706 | if (end > a.length) { |
| 707 | return false; |
| 708 | } |
| 709 | for (int i = start; i < end; i++) { |
| 710 | if (a[i] > value) { |
| 711 | return false; |
| 712 | } |
| 713 | } |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | private static void procrusteanFill(int[] a, int start, int end, int value) { |
| 718 | int length = a.length; |
| 719 | Arrays.fill(a, Math.min(start, length), Math.min(end, length), value); |
| 720 | } |
| 721 | |
| 722 | private static void setCellGroup(LayoutParams lp, int row, int rowSpan, int col, int colSpan) { |
| 723 | lp.setRowSpecSpan(new Interval(row, row + rowSpan)); |
| 724 | lp.setColumnSpecSpan(new Interval(col, col + colSpan)); |
| 725 | } |
| 726 | |
| 727 | // Logic to avert infinite loops by ensuring that the cells can be placed somewhere. |
| 728 | private static int clip(Interval minorRange, boolean minorWasDefined, int count) { |
| 729 | int size = minorRange.size(); |
| 730 | if (count == 0) { |
| 731 | return size; |
| 732 | } |
| 733 | int min = minorWasDefined ? min(minorRange.min, count) : 0; |
| 734 | return min(size, count - min); |
| 735 | } |
| 736 | |
Philip Milne | f474870 | 2011-06-09 18:30:32 -0700 | [diff] [blame] | 737 | // install default indices for cells that don't define them |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 738 | private void validateLayoutParams() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 739 | final boolean horizontal = (mOrientation == HORIZONTAL); |
| 740 | final Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
Jim Miller | 782c04b | 2012-05-02 15:45:23 -0700 | [diff] [blame] | 741 | final int count = (axis.definedCount != UNDEFINED) ? axis.definedCount : 0; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 742 | |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 743 | int major = 0; |
| 744 | int minor = 0; |
| 745 | int[] maxSizes = new int[count]; |
| 746 | |
| 747 | for (int i = 0, N = getChildCount(); i < N; i++) { |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 748 | LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 749 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 750 | final Spec majorSpec = horizontal ? lp.rowSpec : lp.columnSpec; |
| 751 | final Interval majorRange = majorSpec.span; |
| 752 | final boolean majorWasDefined = majorSpec.startDefined; |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 753 | final int majorSpan = majorRange.size(); |
| 754 | if (majorWasDefined) { |
| 755 | major = majorRange.min; |
Philip Milne | f474870 | 2011-06-09 18:30:32 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 758 | final Spec minorSpec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 759 | final Interval minorRange = minorSpec.span; |
| 760 | final boolean minorWasDefined = minorSpec.startDefined; |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 761 | final int minorSpan = clip(minorRange, minorWasDefined, count); |
| 762 | if (minorWasDefined) { |
| 763 | minor = minorRange.min; |
| 764 | } |
Philip Milne | f474870 | 2011-06-09 18:30:32 -0700 | [diff] [blame] | 765 | |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 766 | if (count != 0) { |
| 767 | // Find suitable row/col values when at least one is undefined. |
| 768 | if (!majorWasDefined || !minorWasDefined) { |
| 769 | while (!fits(maxSizes, major, minor, minor + minorSpan)) { |
| 770 | if (minorWasDefined) { |
| 771 | major++; |
| 772 | } else { |
| 773 | if (minor + minorSpan <= count) { |
| 774 | minor++; |
| 775 | } else { |
| 776 | minor = 0; |
| 777 | major++; |
| 778 | } |
Philip Milne | f474870 | 2011-06-09 18:30:32 -0700 | [diff] [blame] | 779 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 780 | } |
| 781 | } |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 782 | procrusteanFill(maxSizes, minor, minor + minorSpan, major + majorSpan); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 783 | } |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 784 | |
| 785 | if (horizontal) { |
| 786 | setCellGroup(lp, major, majorSpan, minor, minorSpan); |
| 787 | } else { |
| 788 | setCellGroup(lp, minor, minorSpan, major, majorSpan); |
| 789 | } |
| 790 | |
| 791 | minor = minor + minorSpan; |
| 792 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | private void invalidateStructure() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 796 | mLastLayoutParamsHashCode = UNINITIALIZED_HASH; |
| 797 | mHorizontalAxis.invalidateStructure(); |
| 798 | mVerticalAxis.invalidateStructure(); |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 799 | // This can end up being done twice. Better twice than not at all. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 800 | invalidateValues(); |
| 801 | } |
| 802 | |
| 803 | private void invalidateValues() { |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 804 | // Need null check because requestLayout() is called in View's initializer, |
| 805 | // before we are set up. |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 806 | if (mHorizontalAxis != null && mVerticalAxis != null) { |
| 807 | mHorizontalAxis.invalidateValues(); |
| 808 | mVerticalAxis.invalidateValues(); |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 809 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 812 | /** @hide */ |
| 813 | @Override |
| 814 | protected void onSetLayoutParams(View child, ViewGroup.LayoutParams layoutParams) { |
| 815 | super.onSetLayoutParams(child, layoutParams); |
Philip Milne | 0f57cea | 2012-05-12 09:34:25 -0700 | [diff] [blame] | 816 | |
| 817 | if (!checkLayoutParams(layoutParams)) { |
| 818 | handleInvalidParams("supplied LayoutParams are of the wrong type"); |
| 819 | } |
| 820 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 821 | invalidateStructure(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 824 | final LayoutParams getLayoutParams(View c) { |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 825 | return (LayoutParams) c.getLayoutParams(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Philip Milne | 0f57cea | 2012-05-12 09:34:25 -0700 | [diff] [blame] | 828 | private static void handleInvalidParams(String msg) { |
| 829 | throw new IllegalArgumentException(msg + ". "); |
| 830 | } |
| 831 | |
| 832 | private void checkLayoutParams(LayoutParams lp, boolean horizontal) { |
| 833 | String groupName = horizontal ? "column" : "row"; |
| 834 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 835 | Interval span = spec.span; |
| 836 | if (span.min != UNDEFINED && span.min < 0) { |
| 837 | handleInvalidParams(groupName + " indices must be positive"); |
| 838 | } |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 839 | Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
Philip Milne | 0f57cea | 2012-05-12 09:34:25 -0700 | [diff] [blame] | 840 | int count = axis.definedCount; |
| 841 | if (count != UNDEFINED) { |
| 842 | if (span.max > count) { |
| 843 | handleInvalidParams(groupName + |
| 844 | " indices (start + span) mustn't exceed the " + groupName + " count"); |
| 845 | } |
| 846 | if (span.size() > count) { |
| 847 | handleInvalidParams(groupName + " span mustn't exceed the " + groupName + " count"); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | @Override |
| 853 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 854 | if (!(p instanceof LayoutParams)) { |
| 855 | return false; |
| 856 | } |
| 857 | LayoutParams lp = (LayoutParams) p; |
| 858 | |
| 859 | checkLayoutParams(lp, true); |
| 860 | checkLayoutParams(lp, false); |
| 861 | |
| 862 | return true; |
| 863 | } |
| 864 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 865 | @Override |
| 866 | protected LayoutParams generateDefaultLayoutParams() { |
| 867 | return new LayoutParams(); |
| 868 | } |
| 869 | |
| 870 | @Override |
| 871 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 872 | return new LayoutParams(getContext(), attrs); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | @Override |
| 876 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 877 | return new LayoutParams(p); |
| 878 | } |
| 879 | |
| 880 | // Draw grid |
| 881 | |
| 882 | private void drawLine(Canvas graphics, int x1, int y1, int x2, int y2, Paint paint) { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 883 | if (isLayoutRtl()) { |
| 884 | int width = getWidth(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 885 | graphics.drawLine(width - x1, y1, width - x2, y2, paint); |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 886 | } else { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 887 | graphics.drawLine(x1, y1, x2, y2, paint); |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 888 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 889 | } |
| 890 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 891 | /** |
| 892 | * @hide |
| 893 | */ |
| 894 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 895 | protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 896 | // Apply defaults, so as to remove UNDEFINED values |
| 897 | LayoutParams lp = new LayoutParams(); |
| 898 | for (int i = 0; i < getChildCount(); i++) { |
| 899 | View c = getChildAt(i); |
| 900 | lp.setMargins( |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 901 | getMargin1(c, true, true), |
| 902 | getMargin1(c, false, true), |
| 903 | getMargin1(c, true, false), |
| 904 | getMargin1(c, false, false)); |
| 905 | lp.onDebugDraw(c, canvas, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 906 | } |
Philip Milne | b559976 | 2011-08-05 11:04:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 909 | /** |
| 910 | * @hide |
| 911 | */ |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 912 | @Override |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 913 | protected void onDebugDraw(Canvas canvas) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 914 | Paint paint = new Paint(); |
| 915 | paint.setStyle(Paint.Style.STROKE); |
| 916 | paint.setColor(Color.argb(50, 255, 255, 255)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 917 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 918 | Insets insets = getOpticalInsets(); |
| 919 | |
| 920 | int top = getPaddingTop() + insets.top; |
| 921 | int left = getPaddingLeft() + insets.left; |
| 922 | int right = getWidth() - getPaddingRight() - insets.right; |
| 923 | int bottom = getHeight() - getPaddingBottom() - insets.bottom; |
| 924 | |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 925 | int[] xs = mHorizontalAxis.locations; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 926 | if (xs != null) { |
| 927 | for (int i = 0, length = xs.length; i < length; i++) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 928 | int x = left + xs[i]; |
| 929 | drawLine(canvas, x, top, x, bottom, paint); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 930 | } |
| 931 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 932 | |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 933 | int[] ys = mVerticalAxis.locations; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 934 | if (ys != null) { |
| 935 | for (int i = 0, length = ys.length; i < length; i++) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 936 | int y = top + ys[i]; |
| 937 | drawLine(canvas, left, y, right, y, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
| 941 | super.onDebugDraw(canvas); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 944 | // Add/remove |
| 945 | |
Philip Milne | e7dda0b | 2011-07-19 10:35:56 -0700 | [diff] [blame] | 946 | /** |
| 947 | * @hide |
| 948 | */ |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 949 | @Override |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 950 | protected void onViewAdded(View child) { |
| 951 | super.onViewAdded(child); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 952 | invalidateStructure(); |
| 953 | } |
| 954 | |
Philip Milne | e7dda0b | 2011-07-19 10:35:56 -0700 | [diff] [blame] | 955 | /** |
| 956 | * @hide |
| 957 | */ |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 958 | @Override |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 959 | protected void onViewRemoved(View child) { |
| 960 | super.onViewRemoved(child); |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 961 | invalidateStructure(); |
| 962 | } |
| 963 | |
Philip Milne | 350f0a6 | 2011-07-19 14:00:56 -0700 | [diff] [blame] | 964 | /** |
| 965 | * We need to call invalidateStructure() when a child's GONE flag changes state. |
| 966 | * This implementation is a catch-all, invalidating on any change in the visibility flags. |
| 967 | * |
| 968 | * @hide |
| 969 | */ |
| 970 | @Override |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 971 | protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
| 972 | super.onChildVisibilityChanged(child, oldVisibility, newVisibility); |
| 973 | if (oldVisibility == GONE || newVisibility == GONE) { |
Philip Milne | a841644 | 2013-02-25 10:49:39 -0800 | [diff] [blame] | 974 | invalidateStructure(); |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 975 | } |
Philip Milne | 350f0a6 | 2011-07-19 14:00:56 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 978 | private int computeLayoutParamsHashCode() { |
| 979 | int result = 1; |
| 980 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 981 | View c = getChildAt(i); |
| 982 | if (c.getVisibility() == View.GONE) continue; |
| 983 | LayoutParams lp = (LayoutParams) c.getLayoutParams(); |
| 984 | result = 31 * result + lp.hashCode(); |
| 985 | } |
| 986 | return result; |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 989 | private void consistencyCheck() { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 990 | if (mLastLayoutParamsHashCode == UNINITIALIZED_HASH) { |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 991 | validateLayoutParams(); |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 992 | mLastLayoutParamsHashCode = computeLayoutParamsHashCode(); |
| 993 | } else if (mLastLayoutParamsHashCode != computeLayoutParamsHashCode()) { |
| 994 | mPrinter.println("The fields of some layout parameters were modified in between " |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 995 | + "layout operations. Check the javadoc for GridLayout.LayoutParams#rowSpec."); |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 996 | invalidateStructure(); |
| 997 | consistencyCheck(); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | // Measurement |
| 1002 | |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1003 | // Note: padding has already been removed from the supplied specs |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1004 | private void measureChildWithMargins2(View child, int parentWidthSpec, int parentHeightSpec, |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 1005 | int childWidth, int childHeight) { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1006 | int childWidthSpec = getChildMeasureSpec(parentWidthSpec, |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1007 | getTotalMargin(child, true), childWidth); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1008 | int childHeightSpec = getChildMeasureSpec(parentHeightSpec, |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1009 | getTotalMargin(child, false), childHeight); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1010 | child.measure(childWidthSpec, childHeightSpec); |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1013 | // Note: padding has already been removed from the supplied specs |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1014 | private void measureChildrenWithMargins(int widthSpec, int heightSpec, boolean firstPass) { |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1015 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1016 | View c = getChildAt(i); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1017 | if (c.getVisibility() == View.GONE) continue; |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1018 | LayoutParams lp = getLayoutParams(c); |
| 1019 | if (firstPass) { |
| 1020 | measureChildWithMargins2(c, widthSpec, heightSpec, lp.width, lp.height); |
| 1021 | } else { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1022 | boolean horizontal = (mOrientation == HORIZONTAL); |
Philip Milne | ecab117 | 2011-10-25 15:07:19 -0700 | [diff] [blame] | 1023 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1024 | if (spec.alignment == FILL) { |
| 1025 | Interval span = spec.span; |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1026 | Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1027 | int[] locations = axis.getLocations(); |
Philip Milne | ecab117 | 2011-10-25 15:07:19 -0700 | [diff] [blame] | 1028 | int cellSize = locations[span.max] - locations[span.min]; |
| 1029 | int viewSize = cellSize - getTotalMargin(c, horizontal); |
| 1030 | if (horizontal) { |
| 1031 | measureChildWithMargins2(c, widthSpec, heightSpec, viewSize, lp.height); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1032 | } else { |
Philip Milne | ecab117 | 2011-10-25 15:07:19 -0700 | [diff] [blame] | 1033 | measureChildWithMargins2(c, widthSpec, heightSpec, lp.width, viewSize); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1040 | static int adjust(int measureSpec, int delta) { |
| 1041 | return makeMeasureSpec( |
| 1042 | MeasureSpec.getSize(measureSpec + delta), MeasureSpec.getMode(measureSpec)); |
| 1043 | } |
| 1044 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1045 | @Override |
| 1046 | protected void onMeasure(int widthSpec, int heightSpec) { |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 1047 | consistencyCheck(); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1048 | |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1049 | /** If we have been called by {@link View#measure(int, int)}, one of width or height |
| 1050 | * is likely to have changed. We must invalidate if so. */ |
| 1051 | invalidateValues(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1052 | |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1053 | int hPadding = getPaddingLeft() + getPaddingRight(); |
| 1054 | int vPadding = getPaddingTop() + getPaddingBottom(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1055 | |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1056 | int widthSpecSansPadding = adjust( widthSpec, -hPadding); |
| 1057 | int heightSpecSansPadding = adjust(heightSpec, -vPadding); |
| 1058 | |
| 1059 | measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, true); |
| 1060 | |
| 1061 | int widthSansPadding; |
| 1062 | int heightSansPadding; |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1063 | |
| 1064 | // Use the orientation property to decide which axis should be laid out first. |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1065 | if (mOrientation == HORIZONTAL) { |
| 1066 | widthSansPadding = mHorizontalAxis.getMeasure(widthSpecSansPadding); |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1067 | measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, false); |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1068 | heightSansPadding = mVerticalAxis.getMeasure(heightSpecSansPadding); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1069 | } else { |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1070 | heightSansPadding = mVerticalAxis.getMeasure(heightSpecSansPadding); |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1071 | measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, false); |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1072 | widthSansPadding = mHorizontalAxis.getMeasure(widthSpecSansPadding); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1075 | int measuredWidth = Math.max(widthSansPadding + hPadding, getSuggestedMinimumWidth()); |
| 1076 | int measuredHeight = Math.max(heightSansPadding + vPadding, getSuggestedMinimumHeight()); |
Philip Milne | 09e2d4d | 2011-06-20 10:28:18 -0700 | [diff] [blame] | 1077 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1078 | setMeasuredDimension( |
Philip Milne | e0b85cd | 2013-04-12 14:38:34 -0700 | [diff] [blame] | 1079 | resolveSizeAndState(measuredWidth, widthSpec, 0), |
Philip Milne | 09e2d4d | 2011-06-20 10:28:18 -0700 | [diff] [blame] | 1080 | resolveSizeAndState(measuredHeight, heightSpec, 0)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1083 | private int getMeasurement(View c, boolean horizontal) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 1084 | return horizontal ? c.getMeasuredWidth() : c.getMeasuredHeight(); |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1085 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1086 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1087 | final int getMeasurementIncludingMargin(View c, boolean horizontal) { |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1088 | if (c.getVisibility() == View.GONE) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1089 | return 0; |
| 1090 | } |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1091 | return getMeasurement(c, horizontal) + getTotalMargin(c, horizontal); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1094 | @Override |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1095 | public void requestLayout() { |
| 1096 | super.requestLayout(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1097 | invalidateValues(); |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1098 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1099 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1100 | final Alignment getAlignment(Alignment alignment, boolean horizontal) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1101 | return (alignment != UNDEFINED_ALIGNMENT) ? alignment : |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 1102 | (horizontal ? START : BASELINE); |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1105 | // Layout container |
| 1106 | |
| 1107 | /** |
| 1108 | * {@inheritDoc} |
| 1109 | */ |
| 1110 | /* |
| 1111 | The layout operation is implemented by delegating the heavy lifting to the |
| 1112 | to the mHorizontalAxis and mVerticalAxis instances of the internal Axis class. |
| 1113 | Together they compute the locations of the vertical and horizontal lines of |
| 1114 | the grid (respectively!). |
| 1115 | |
| 1116 | This method is then left with the simpler task of applying margins, gravity |
| 1117 | and sizing to each child view and then placing it in its cell. |
| 1118 | */ |
| 1119 | @Override |
Philip Milne | 09e2d4d | 2011-06-20 10:28:18 -0700 | [diff] [blame] | 1120 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 1121 | consistencyCheck(); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1122 | |
Philip Milne | 09e2d4d | 2011-06-20 10:28:18 -0700 | [diff] [blame] | 1123 | int targetWidth = right - left; |
| 1124 | int targetHeight = bottom - top; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1125 | |
| 1126 | int paddingLeft = getPaddingLeft(); |
| 1127 | int paddingTop = getPaddingTop(); |
| 1128 | int paddingRight = getPaddingRight(); |
| 1129 | int paddingBottom = getPaddingBottom(); |
| 1130 | |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1131 | mHorizontalAxis.layout(targetWidth - paddingLeft - paddingRight); |
| 1132 | mVerticalAxis.layout(targetHeight - paddingTop - paddingBottom); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1133 | |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1134 | int[] hLocations = mHorizontalAxis.getLocations(); |
| 1135 | int[] vLocations = mVerticalAxis.getLocations(); |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1136 | |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1137 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1138 | View c = getChildAt(i); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1139 | if (c.getVisibility() == View.GONE) continue; |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1140 | LayoutParams lp = getLayoutParams(c); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1141 | Spec columnSpec = lp.columnSpec; |
| 1142 | Spec rowSpec = lp.rowSpec; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1143 | |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1144 | Interval colSpan = columnSpec.span; |
| 1145 | Interval rowSpan = rowSpec.span; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1146 | |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1147 | int x1 = hLocations[colSpan.min]; |
| 1148 | int y1 = vLocations[rowSpan.min]; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1149 | |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1150 | int x2 = hLocations[colSpan.max]; |
| 1151 | int y2 = vLocations[rowSpan.max]; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1152 | |
| 1153 | int cellWidth = x2 - x1; |
| 1154 | int cellHeight = y2 - y1; |
| 1155 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1156 | int pWidth = getMeasurement(c, true); |
| 1157 | int pHeight = getMeasurement(c, false); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1158 | |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1159 | Alignment hAlign = getAlignment(columnSpec.alignment, true); |
| 1160 | Alignment vAlign = getAlignment(rowSpec.alignment, false); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1161 | |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1162 | Bounds boundsX = mHorizontalAxis.getGroupBounds().getValue(i); |
| 1163 | Bounds boundsY = mVerticalAxis.getGroupBounds().getValue(i); |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1164 | |
| 1165 | // Gravity offsets: the location of the alignment group relative to its cell group. |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1166 | int gravityOffsetX = hAlign.getGravityOffset(c, cellWidth - boundsX.size(true)); |
| 1167 | int gravityOffsetY = vAlign.getGravityOffset(c, cellHeight - boundsY.size(true)); |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1168 | |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1169 | int leftMargin = getMargin(c, true, true); |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1170 | int topMargin = getMargin(c, false, true); |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1171 | int rightMargin = getMargin(c, true, false); |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1172 | int bottomMargin = getMargin(c, false, false); |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1173 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 1174 | int sumMarginsX = leftMargin + rightMargin; |
| 1175 | int sumMarginsY = topMargin + bottomMargin; |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1176 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 1177 | // Alignment offsets: the location of the view relative to its alignment group. |
| 1178 | int alignmentOffsetX = boundsX.getOffset(this, c, hAlign, pWidth + sumMarginsX, true); |
| 1179 | int alignmentOffsetY = boundsY.getOffset(this, c, vAlign, pHeight + sumMarginsY, false); |
| 1180 | |
| 1181 | int width = hAlign.getSizeInCell(c, pWidth, cellWidth - sumMarginsX); |
| 1182 | int height = vAlign.getSizeInCell(c, pHeight, cellHeight - sumMarginsY); |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1183 | |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1184 | int dx = x1 + gravityOffsetX + alignmentOffsetX; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1185 | |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1186 | int cx = !isLayoutRtl() ? paddingLeft + leftMargin + dx : |
| 1187 | targetWidth - width - paddingRight - rightMargin - dx; |
| 1188 | int cy = paddingTop + y1 + gravityOffsetY + alignmentOffsetY + topMargin; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1189 | |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1190 | if (width != c.getMeasuredWidth() || height != c.getMeasuredHeight()) { |
| 1191 | c.measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY)); |
| 1192 | } |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1193 | c.layout(cx, cy, cx + width, cy + height); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1197 | @Override |
| 1198 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1199 | super.onInitializeAccessibilityEvent(event); |
| 1200 | event.setClassName(GridLayout.class.getName()); |
| 1201 | } |
| 1202 | |
| 1203 | @Override |
| 1204 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1205 | super.onInitializeAccessibilityNodeInfo(info); |
| 1206 | info.setClassName(GridLayout.class.getName()); |
| 1207 | } |
| 1208 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1209 | // Inner classes |
| 1210 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1211 | /* |
| 1212 | This internal class houses the algorithm for computing the locations of grid lines; |
| 1213 | along either the horizontal or vertical axis. A GridLayout uses two instances of this class - |
| 1214 | distinguished by the "horizontal" flag which is true for the horizontal axis and false |
| 1215 | for the vertical one. |
| 1216 | */ |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1217 | final class Axis { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1218 | private static final int NEW = 0; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1219 | private static final int PENDING = 1; |
| 1220 | private static final int COMPLETE = 2; |
| 1221 | |
| 1222 | public final boolean horizontal; |
| 1223 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1224 | public int definedCount = UNDEFINED; |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1225 | private int maxIndex = UNDEFINED; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1226 | |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1227 | PackedMap<Spec, Bounds> groupBounds; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1228 | public boolean groupBoundsValid = false; |
| 1229 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1230 | PackedMap<Interval, MutableInt> forwardLinks; |
| 1231 | public boolean forwardLinksValid = false; |
| 1232 | |
| 1233 | PackedMap<Interval, MutableInt> backwardLinks; |
| 1234 | public boolean backwardLinksValid = false; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1235 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1236 | public int[] leadingMargins; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1237 | public boolean leadingMarginsValid = false; |
| 1238 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1239 | public int[] trailingMargins; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1240 | public boolean trailingMarginsValid = false; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1241 | |
| 1242 | public Arc[] arcs; |
| 1243 | public boolean arcsValid = false; |
| 1244 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1245 | public int[] locations; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1246 | public boolean locationsValid = false; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1247 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1248 | boolean orderPreserved = DEFAULT_ORDER_PRESERVED; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1249 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1250 | private MutableInt parentMin = new MutableInt(0); |
| 1251 | private MutableInt parentMax = new MutableInt(-MAX_SIZE); |
| 1252 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1253 | private Axis(boolean horizontal) { |
| 1254 | this.horizontal = horizontal; |
| 1255 | } |
| 1256 | |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1257 | private int calculateMaxIndex() { |
| 1258 | // the number Integer.MIN_VALUE + 1 comes up in undefined cells |
| 1259 | int result = -1; |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1260 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1261 | View c = getChildAt(i); |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1262 | LayoutParams params = getLayoutParams(c); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1263 | Spec spec = horizontal ? params.columnSpec : params.rowSpec; |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1264 | Interval span = spec.span; |
| 1265 | result = max(result, span.min); |
| 1266 | result = max(result, span.max); |
Philip Milne | 0f57cea | 2012-05-12 09:34:25 -0700 | [diff] [blame] | 1267 | result = max(result, span.size()); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1268 | } |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1269 | return result == -1 ? UNDEFINED : result; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1272 | private int getMaxIndex() { |
| 1273 | if (maxIndex == UNDEFINED) { |
| 1274 | maxIndex = max(0, calculateMaxIndex()); // use zero when there are no children |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1275 | } |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1276 | return maxIndex; |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | public int getCount() { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1280 | return max(definedCount, getMaxIndex()); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | public void setCount(int count) { |
Philip Milne | 0f57cea | 2012-05-12 09:34:25 -0700 | [diff] [blame] | 1284 | if (count != UNDEFINED && count < getMaxIndex()) { |
| 1285 | handleInvalidParams((horizontal ? "column" : "row") + |
| 1286 | "Count must be greater than or equal to the maximum of all grid indices " + |
| 1287 | "(and spans) defined in the LayoutParams of each child"); |
| 1288 | } |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1289 | this.definedCount = count; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | public boolean isOrderPreserved() { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1293 | return orderPreserved; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | public void setOrderPreserved(boolean orderPreserved) { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1297 | this.orderPreserved = orderPreserved; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1298 | invalidateStructure(); |
| 1299 | } |
| 1300 | |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1301 | private PackedMap<Spec, Bounds> createGroupBounds() { |
| 1302 | Assoc<Spec, Bounds> assoc = Assoc.of(Spec.class, Bounds.class); |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1303 | for (int i = 0, N = getChildCount(); i < N; i++) { |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1304 | View c = getChildAt(i); |
Philip Milne | a841644 | 2013-02-25 10:49:39 -0800 | [diff] [blame] | 1305 | // we must include views that are GONE here, see introductory javadoc |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1306 | LayoutParams lp = getLayoutParams(c); |
| 1307 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1308 | Bounds bounds = getAlignment(spec.alignment, horizontal).getBounds(); |
| 1309 | assoc.put(spec, bounds); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1310 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1311 | return assoc.pack(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | private void computeGroupBounds() { |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1315 | Bounds[] values = groupBounds.values; |
| 1316 | for (int i = 0; i < values.length; i++) { |
| 1317 | values[i].reset(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1318 | } |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1319 | for (int i = 0, N = getChildCount(); i < N; i++) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1320 | View c = getChildAt(i); |
Philip Milne | a841644 | 2013-02-25 10:49:39 -0800 | [diff] [blame] | 1321 | // we must include views that are GONE here, see introductory javadoc |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1322 | LayoutParams lp = getLayoutParams(c); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1323 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 1324 | groupBounds.getValue(i).include(GridLayout.this, c, spec, this); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1328 | public PackedMap<Spec, Bounds> getGroupBounds() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1329 | if (groupBounds == null) { |
| 1330 | groupBounds = createGroupBounds(); |
| 1331 | } |
| 1332 | if (!groupBoundsValid) { |
| 1333 | computeGroupBounds(); |
| 1334 | groupBoundsValid = true; |
| 1335 | } |
| 1336 | return groupBounds; |
| 1337 | } |
| 1338 | |
| 1339 | // Add values computed by alignment - taking the max of all alignments in each span |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1340 | private PackedMap<Interval, MutableInt> createLinks(boolean min) { |
| 1341 | Assoc<Interval, MutableInt> result = Assoc.of(Interval.class, MutableInt.class); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1342 | Spec[] keys = getGroupBounds().keys; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1343 | for (int i = 0, N = keys.length; i < N; i++) { |
| 1344 | Interval span = min ? keys[i].span : keys[i].span.inverse(); |
| 1345 | result.put(span, new MutableInt()); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1346 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1347 | return result.pack(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1350 | private void computeLinks(PackedMap<Interval, MutableInt> links, boolean min) { |
| 1351 | MutableInt[] spans = links.values; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1352 | for (int i = 0; i < spans.length; i++) { |
| 1353 | spans[i].reset(); |
| 1354 | } |
| 1355 | |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 1356 | // Use getter to trigger a re-evaluation |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1357 | Bounds[] bounds = getGroupBounds().values; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1358 | for (int i = 0; i < bounds.length; i++) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1359 | int size = bounds[i].size(min); |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1360 | MutableInt valueHolder = links.getValue(i); |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1361 | // this effectively takes the max() of the minima and the min() of the maxima |
| 1362 | valueHolder.value = max(valueHolder.value, min ? size : -size); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1366 | private PackedMap<Interval, MutableInt> getForwardLinks() { |
| 1367 | if (forwardLinks == null) { |
| 1368 | forwardLinks = createLinks(true); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1369 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1370 | if (!forwardLinksValid) { |
| 1371 | computeLinks(forwardLinks, true); |
| 1372 | forwardLinksValid = true; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1373 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1374 | return forwardLinks; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1377 | private PackedMap<Interval, MutableInt> getBackwardLinks() { |
| 1378 | if (backwardLinks == null) { |
| 1379 | backwardLinks = createLinks(false); |
| 1380 | } |
| 1381 | if (!backwardLinksValid) { |
| 1382 | computeLinks(backwardLinks, false); |
| 1383 | backwardLinksValid = true; |
| 1384 | } |
| 1385 | return backwardLinks; |
| 1386 | } |
| 1387 | |
| 1388 | private void include(List<Arc> arcs, Interval key, MutableInt size, |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 1389 | boolean ignoreIfAlreadyPresent) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1390 | /* |
| 1391 | Remove self referential links. |
| 1392 | These appear: |
| 1393 | . as parental constraints when GridLayout has no children |
| 1394 | . when components have been marked as GONE |
| 1395 | */ |
| 1396 | if (key.size() == 0) { |
| 1397 | return; |
| 1398 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1399 | // this bit below should really be computed outside here - |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1400 | // its just to stop default (row/col > 0) constraints obliterating valid entries |
| 1401 | if (ignoreIfAlreadyPresent) { |
| 1402 | for (Arc arc : arcs) { |
| 1403 | Interval span = arc.span; |
| 1404 | if (span.equals(key)) { |
| 1405 | return; |
| 1406 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | arcs.add(new Arc(key, size)); |
| 1410 | } |
| 1411 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1412 | private void include(List<Arc> arcs, Interval key, MutableInt size) { |
| 1413 | include(arcs, key, size, true); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1416 | // Group arcs by their first vertex, returning an array of arrays. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1417 | // This is linear in the number of arcs. |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1418 | Arc[][] groupArcsByFirstVertex(Arc[] arcs) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1419 | int N = getCount() + 1; // the number of vertices |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1420 | Arc[][] result = new Arc[N][]; |
| 1421 | int[] sizes = new int[N]; |
| 1422 | for (Arc arc : arcs) { |
| 1423 | sizes[arc.span.min]++; |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 1424 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1425 | for (int i = 0; i < sizes.length; i++) { |
| 1426 | result[i] = new Arc[sizes[i]]; |
| 1427 | } |
| 1428 | // reuse the sizes array to hold the current last elements as we insert each arc |
| 1429 | Arrays.fill(sizes, 0); |
| 1430 | for (Arc arc : arcs) { |
| 1431 | int i = arc.span.min; |
| 1432 | result[i][sizes[i]++] = arc; |
| 1433 | } |
| 1434 | |
| 1435 | return result; |
| 1436 | } |
| 1437 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1438 | private Arc[] topologicalSort(final Arc[] arcs) { |
| 1439 | return new Object() { |
| 1440 | Arc[] result = new Arc[arcs.length]; |
| 1441 | int cursor = result.length - 1; |
| 1442 | Arc[][] arcsByVertex = groupArcsByFirstVertex(arcs); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1443 | int[] visited = new int[getCount() + 1]; |
| 1444 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1445 | void walk(int loc) { |
| 1446 | switch (visited[loc]) { |
| 1447 | case NEW: { |
| 1448 | visited[loc] = PENDING; |
| 1449 | for (Arc arc : arcsByVertex[loc]) { |
| 1450 | walk(arc.span.max); |
| 1451 | result[cursor--] = arc; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1452 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1453 | visited[loc] = COMPLETE; |
| 1454 | break; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1455 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1456 | case PENDING: { |
Philip Milne | b65408f | 2012-05-21 10:44:46 -0700 | [diff] [blame] | 1457 | // le singe est dans l'arbre |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1458 | assert false; |
| 1459 | break; |
| 1460 | } |
| 1461 | case COMPLETE: { |
| 1462 | break; |
| 1463 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1464 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1465 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1466 | |
| 1467 | Arc[] sort() { |
| 1468 | for (int loc = 0, N = arcsByVertex.length; loc < N; loc++) { |
| 1469 | walk(loc); |
| 1470 | } |
| 1471 | assert cursor == -1; |
| 1472 | return result; |
| 1473 | } |
| 1474 | }.sort(); |
| 1475 | } |
| 1476 | |
| 1477 | private Arc[] topologicalSort(List<Arc> arcs) { |
| 1478 | return topologicalSort(arcs.toArray(new Arc[arcs.size()])); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1479 | } |
| 1480 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1481 | private void addComponentSizes(List<Arc> result, PackedMap<Interval, MutableInt> links) { |
| 1482 | for (int i = 0; i < links.keys.length; i++) { |
| 1483 | Interval key = links.keys[i]; |
| 1484 | include(result, key, links.values[i], false); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1485 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | private Arc[] createArcs() { |
| 1489 | List<Arc> mins = new ArrayList<Arc>(); |
| 1490 | List<Arc> maxs = new ArrayList<Arc>(); |
| 1491 | |
| 1492 | // Add the minimum values from the components. |
| 1493 | addComponentSizes(mins, getForwardLinks()); |
| 1494 | // Add the maximum values from the components. |
| 1495 | addComponentSizes(maxs, getBackwardLinks()); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1496 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1497 | // Add ordering constraints to prevent row/col sizes from going negative |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1498 | if (orderPreserved) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1499 | // Add a constraint for every row/col |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1500 | for (int i = 0; i < getCount(); i++) { |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1501 | include(mins, new Interval(i, i + 1), new MutableInt(0)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1502 | } |
| 1503 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1504 | |
| 1505 | // Add the container constraints. Use the version of include that allows |
| 1506 | // duplicate entries in case a child spans the entire grid. |
| 1507 | int N = getCount(); |
| 1508 | include(mins, new Interval(0, N), parentMin, false); |
| 1509 | include(maxs, new Interval(N, 0), parentMax, false); |
| 1510 | |
| 1511 | // Sort |
| 1512 | Arc[] sMins = topologicalSort(mins); |
| 1513 | Arc[] sMaxs = topologicalSort(maxs); |
| 1514 | |
| 1515 | return append(sMins, sMaxs); |
| 1516 | } |
| 1517 | |
| 1518 | private void computeArcs() { |
| 1519 | // getting the links validates the values that are shared by the arc list |
| 1520 | getForwardLinks(); |
| 1521 | getBackwardLinks(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1522 | } |
| 1523 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1524 | public Arc[] getArcs() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1525 | if (arcs == null) { |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1526 | arcs = createArcs(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1527 | } |
| 1528 | if (!arcsValid) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1529 | computeArcs(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1530 | arcsValid = true; |
| 1531 | } |
| 1532 | return arcs; |
| 1533 | } |
| 1534 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1535 | private boolean relax(int[] locations, Arc entry) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1536 | if (!entry.valid) { |
| 1537 | return false; |
| 1538 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1539 | Interval span = entry.span; |
| 1540 | int u = span.min; |
| 1541 | int v = span.max; |
| 1542 | int value = entry.value.value; |
| 1543 | int candidate = locations[u] + value; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1544 | if (candidate > locations[v]) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1545 | locations[v] = candidate; |
| 1546 | return true; |
| 1547 | } |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1551 | private void init(int[] locations) { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1552 | Arrays.fill(locations, 0); |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | private String arcsToString(List<Arc> arcs) { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1556 | String var = horizontal ? "x" : "y"; |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1557 | StringBuilder result = new StringBuilder(); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1558 | boolean first = true; |
| 1559 | for (Arc arc : arcs) { |
| 1560 | if (first) { |
| 1561 | first = false; |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1562 | } else { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1563 | result = result.append(", "); |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1564 | } |
| 1565 | int src = arc.span.min; |
| 1566 | int dst = arc.span.max; |
| 1567 | int value = arc.value.value; |
| 1568 | result.append((src < dst) ? |
Philip Milne | edd6951 | 2012-03-14 17:21:33 -0700 | [diff] [blame] | 1569 | var + dst + "-" + var + src + ">=" + value : |
| 1570 | var + src + "-" + var + dst + "<=" + -value); |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1571 | |
| 1572 | } |
| 1573 | return result.toString(); |
| 1574 | } |
| 1575 | |
| 1576 | private void logError(String axisName, Arc[] arcs, boolean[] culprits0) { |
| 1577 | List<Arc> culprits = new ArrayList<Arc>(); |
| 1578 | List<Arc> removed = new ArrayList<Arc>(); |
| 1579 | for (int c = 0; c < arcs.length; c++) { |
| 1580 | Arc arc = arcs[c]; |
| 1581 | if (culprits0[c]) { |
| 1582 | culprits.add(arc); |
| 1583 | } |
| 1584 | if (!arc.valid) { |
| 1585 | removed.add(arc); |
| 1586 | } |
| 1587 | } |
Adam Powell | 465ea74 | 2013-08-29 14:56:51 -0700 | [diff] [blame] | 1588 | mPrinter.println(axisName + " constraints: " + arcsToString(culprits) + |
Philip Milne | 211d033 | 2012-04-24 14:45:14 -0700 | [diff] [blame] | 1589 | " are inconsistent; permanently removing: " + arcsToString(removed) + ". "); |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1592 | /* |
| 1593 | Bellman-Ford variant - modified to reduce typical running time from O(N^2) to O(N) |
| 1594 | |
| 1595 | GridLayout converts its requirements into a system of linear constraints of the |
| 1596 | form: |
| 1597 | |
| 1598 | x[i] - x[j] < a[k] |
| 1599 | |
| 1600 | Where the x[i] are variables and the a[k] are constants. |
| 1601 | |
| 1602 | For example, if the variables were instead labeled x, y, z we might have: |
| 1603 | |
| 1604 | x - y < 17 |
| 1605 | y - z < 23 |
| 1606 | z - x < 42 |
| 1607 | |
| 1608 | This is a special case of the Linear Programming problem that is, in turn, |
| 1609 | equivalent to the single-source shortest paths problem on a digraph, for |
| 1610 | which the O(n^2) Bellman-Ford algorithm the most commonly used general solution. |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1611 | */ |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1612 | private void solve(Arc[] arcs, int[] locations) { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1613 | String axisName = horizontal ? "horizontal" : "vertical"; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1614 | int N = getCount() + 1; // The number of vertices is the number of columns/rows + 1. |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1615 | boolean[] originalCulprits = null; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1616 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1617 | for (int p = 0; p < arcs.length; p++) { |
| 1618 | init(locations); |
| 1619 | |
| 1620 | // We take one extra pass over traditional Bellman-Ford (and omit their final step) |
| 1621 | for (int i = 0; i < N; i++) { |
| 1622 | boolean changed = false; |
| 1623 | for (int j = 0, length = arcs.length; j < length; j++) { |
| 1624 | changed |= relax(locations, arcs[j]); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1625 | } |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1626 | if (!changed) { |
| 1627 | if (originalCulprits != null) { |
| 1628 | logError(axisName, arcs, originalCulprits); |
| 1629 | } |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1630 | return; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1631 | } |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | boolean[] culprits = new boolean[arcs.length]; |
| 1635 | for (int i = 0; i < N; i++) { |
| 1636 | for (int j = 0, length = arcs.length; j < length; j++) { |
| 1637 | culprits[j] |= relax(locations, arcs[j]); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | if (p == 0) { |
| 1642 | originalCulprits = culprits; |
| 1643 | } |
| 1644 | |
| 1645 | for (int i = 0; i < arcs.length; i++) { |
| 1646 | if (culprits[i]) { |
| 1647 | Arc arc = arcs[i]; |
| 1648 | // Only remove max values, min values alone cannot be inconsistent |
| 1649 | if (arc.span.min < arc.span.max) { |
| 1650 | continue; |
| 1651 | } |
| 1652 | arc.valid = false; |
| 1653 | break; |
| 1654 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1655 | } |
| 1656 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1659 | private void computeMargins(boolean leading) { |
| 1660 | int[] margins = leading ? leadingMargins : trailingMargins; |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 1661 | for (int i = 0, N = getChildCount(); i < N; i++) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1662 | View c = getChildAt(i); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1663 | if (c.getVisibility() == View.GONE) continue; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1664 | LayoutParams lp = getLayoutParams(c); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1665 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1666 | Interval span = spec.span; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1667 | int index = leading ? span.min : span.max; |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 1668 | margins[index] = max(margins[index], getMargin1(c, horizontal, leading)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1669 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1670 | } |
| 1671 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1672 | // External entry points |
| 1673 | |
| 1674 | public int[] getLeadingMargins() { |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1675 | if (leadingMargins == null) { |
| 1676 | leadingMargins = new int[getCount() + 1]; |
| 1677 | } |
| 1678 | if (!leadingMarginsValid) { |
| 1679 | computeMargins(true); |
| 1680 | leadingMarginsValid = true; |
| 1681 | } |
| 1682 | return leadingMargins; |
| 1683 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1684 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1685 | public int[] getTrailingMargins() { |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1686 | if (trailingMargins == null) { |
| 1687 | trailingMargins = new int[getCount() + 1]; |
| 1688 | } |
| 1689 | if (!trailingMarginsValid) { |
| 1690 | computeMargins(false); |
| 1691 | trailingMarginsValid = true; |
| 1692 | } |
| 1693 | return trailingMargins; |
| 1694 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1695 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1696 | private void computeLocations(int[] a) { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1697 | solve(getArcs(), a); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1698 | if (!orderPreserved) { |
| 1699 | // Solve returns the smallest solution to the constraint system for which all |
| 1700 | // values are positive. One value is therefore zero - though if the row/col |
| 1701 | // order is not preserved this may not be the first vertex. For consistency, |
| 1702 | // translate all the values so that they measure the distance from a[0]; the |
| 1703 | // leading edge of the parent. After this transformation some values may be |
| 1704 | // negative. |
| 1705 | int a0 = a[0]; |
| 1706 | for (int i = 0, N = a.length; i < N; i++) { |
| 1707 | a[i] = a[i] - a0; |
| 1708 | } |
| 1709 | } |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1712 | public int[] getLocations() { |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1713 | if (locations == null) { |
| 1714 | int N = getCount() + 1; |
| 1715 | locations = new int[N]; |
| 1716 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1717 | if (!locationsValid) { |
| 1718 | computeLocations(locations); |
| 1719 | locationsValid = true; |
| 1720 | } |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1721 | return locations; |
| 1722 | } |
| 1723 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1724 | private int size(int[] locations) { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1725 | // The parental edges are attached to vertices 0 and N - even when order is not |
| 1726 | // being preserved and other vertices fall outside this range. Measure the distance |
| 1727 | // between vertices 0 and N, assuming that locations[0] = 0. |
| 1728 | return locations[getCount()]; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1729 | } |
| 1730 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1731 | private void setParentConstraints(int min, int max) { |
| 1732 | parentMin.value = min; |
| 1733 | parentMax.value = -max; |
| 1734 | locationsValid = false; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1737 | private int getMeasure(int min, int max) { |
| 1738 | setParentConstraints(min, max); |
| 1739 | return size(getLocations()); |
| 1740 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1741 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1742 | public int getMeasure(int measureSpec) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1743 | int mode = MeasureSpec.getMode(measureSpec); |
| 1744 | int size = MeasureSpec.getSize(measureSpec); |
| 1745 | switch (mode) { |
| 1746 | case MeasureSpec.UNSPECIFIED: { |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1747 | return getMeasure(0, MAX_SIZE); |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1748 | } |
| 1749 | case MeasureSpec.EXACTLY: { |
| 1750 | return getMeasure(size, size); |
| 1751 | } |
| 1752 | case MeasureSpec.AT_MOST: { |
| 1753 | return getMeasure(0, size); |
| 1754 | } |
| 1755 | default: { |
| 1756 | assert false; |
| 1757 | return 0; |
| 1758 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1759 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1760 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1761 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1762 | public void layout(int size) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1763 | setParentConstraints(size, size); |
| 1764 | getLocations(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1765 | } |
| 1766 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1767 | public void invalidateStructure() { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 1768 | maxIndex = UNDEFINED; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1769 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1770 | groupBounds = null; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1771 | forwardLinks = null; |
| 1772 | backwardLinks = null; |
| 1773 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1774 | leadingMargins = null; |
| 1775 | trailingMargins = null; |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 1776 | arcs = null; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1777 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1778 | locations = null; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1779 | |
| 1780 | invalidateValues(); |
| 1781 | } |
| 1782 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1783 | public void invalidateValues() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1784 | groupBoundsValid = false; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1785 | forwardLinksValid = false; |
| 1786 | backwardLinksValid = false; |
| 1787 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1788 | leadingMarginsValid = false; |
| 1789 | trailingMarginsValid = false; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 1790 | arcsValid = false; |
| 1791 | |
| 1792 | locationsValid = false; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * Layout information associated with each of the children of a GridLayout. |
| 1798 | * <p> |
| 1799 | * GridLayout supports both row and column spanning and arbitrary forms of alignment within |
| 1800 | * each cell group. The fundamental parameters associated with each cell group are |
| 1801 | * gathered into their vertical and horizontal components and stored |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1802 | * in the {@link #rowSpec} and {@link #columnSpec} layout parameters. |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1803 | * {@link GridLayout.Spec Specs} are immutable structures |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 1804 | * and may be shared between the layout parameters of different children. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1805 | * <p> |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1806 | * The row and column specs contain the leading and trailing indices along each axis |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 1807 | * and together specify the four grid indices that delimit the cells of this cell group. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1808 | * <p> |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1809 | * The alignment properties of the row and column specs together specify |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1810 | * both aspects of alignment within the cell group. It is also possible to specify a child's |
| 1811 | * alignment within its cell group by using the {@link GridLayout.LayoutParams#setGravity(int)} |
| 1812 | * method. |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1813 | * |
| 1814 | * <h4>WRAP_CONTENT and MATCH_PARENT</h4> |
| 1815 | * |
| 1816 | * Because the default values of the {@link #width} and {@link #height} |
| 1817 | * properties are both {@link #WRAP_CONTENT}, this value never needs to be explicitly |
| 1818 | * declared in the layout parameters of GridLayout's children. In addition, |
| 1819 | * GridLayout does not distinguish the special size value {@link #MATCH_PARENT} from |
| 1820 | * {@link #WRAP_CONTENT}. A component's ability to expand to the size of the parent is |
| 1821 | * instead controlled by the principle of <em>flexibility</em>, |
| 1822 | * as discussed in {@link GridLayout}. |
| 1823 | * |
| 1824 | * <h4>Summary</h4> |
| 1825 | * |
| 1826 | * You should not need to use either of the special size values: |
| 1827 | * {@code WRAP_CONTENT} or {@code MATCH_PARENT} when configuring the children of |
| 1828 | * a GridLayout. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1829 | * |
| 1830 | * <h4>Default values</h4> |
| 1831 | * |
| 1832 | * <ul> |
| 1833 | * <li>{@link #width} = {@link #WRAP_CONTENT}</li> |
| 1834 | * <li>{@link #height} = {@link #WRAP_CONTENT}</li> |
| 1835 | * <li>{@link #topMargin} = 0 when |
| 1836 | * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1837 | * {@code false}; otherwise {@link #UNDEFINED}, to |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1838 | * indicate that a default value should be computed on demand. </li> |
| 1839 | * <li>{@link #leftMargin} = 0 when |
| 1840 | * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1841 | * {@code false}; otherwise {@link #UNDEFINED}, to |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1842 | * indicate that a default value should be computed on demand. </li> |
| 1843 | * <li>{@link #bottomMargin} = 0 when |
| 1844 | * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1845 | * {@code false}; otherwise {@link #UNDEFINED}, to |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1846 | * indicate that a default value should be computed on demand. </li> |
| 1847 | * <li>{@link #rightMargin} = 0 when |
| 1848 | * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 1849 | * {@code false}; otherwise {@link #UNDEFINED}, to |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1850 | * indicate that a default value should be computed on demand. </li> |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1851 | * <li>{@link #rowSpec}<code>.row</code> = {@link #UNDEFINED} </li> |
| 1852 | * <li>{@link #rowSpec}<code>.rowSpan</code> = 1 </li> |
| 1853 | * <li>{@link #rowSpec}<code>.alignment</code> = {@link #BASELINE} </li> |
| 1854 | * <li>{@link #columnSpec}<code>.column</code> = {@link #UNDEFINED} </li> |
| 1855 | * <li>{@link #columnSpec}<code>.columnSpan</code> = 1 </li> |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 1856 | * <li>{@link #columnSpec}<code>.alignment</code> = {@link #START} </li> |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1857 | * </ul> |
| 1858 | * |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1859 | * See {@link GridLayout} for a more complete description of the conventions |
| 1860 | * used by GridLayout in the interpretation of the properties of this class. |
| 1861 | * |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1862 | * @attr ref android.R.styleable#GridLayout_Layout_layout_row |
| 1863 | * @attr ref android.R.styleable#GridLayout_Layout_layout_rowSpan |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1864 | * @attr ref android.R.styleable#GridLayout_Layout_layout_column |
| 1865 | * @attr ref android.R.styleable#GridLayout_Layout_layout_columnSpan |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1866 | * @attr ref android.R.styleable#GridLayout_Layout_layout_gravity |
| 1867 | */ |
| 1868 | public static class LayoutParams extends MarginLayoutParams { |
| 1869 | |
| 1870 | // Default values |
| 1871 | |
| 1872 | private static final int DEFAULT_WIDTH = WRAP_CONTENT; |
| 1873 | private static final int DEFAULT_HEIGHT = WRAP_CONTENT; |
| 1874 | private static final int DEFAULT_MARGIN = UNDEFINED; |
| 1875 | private static final int DEFAULT_ROW = UNDEFINED; |
| 1876 | private static final int DEFAULT_COLUMN = UNDEFINED; |
Philip Milne | f474870 | 2011-06-09 18:30:32 -0700 | [diff] [blame] | 1877 | private static final Interval DEFAULT_SPAN = new Interval(UNDEFINED, UNDEFINED + 1); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1878 | private static final int DEFAULT_SPAN_SIZE = DEFAULT_SPAN.size(); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1879 | |
| 1880 | // TypedArray indices |
| 1881 | |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 1882 | private static final int MARGIN = R.styleable.ViewGroup_MarginLayout_layout_margin; |
| 1883 | private static final int LEFT_MARGIN = R.styleable.ViewGroup_MarginLayout_layout_marginLeft; |
| 1884 | private static final int TOP_MARGIN = R.styleable.ViewGroup_MarginLayout_layout_marginTop; |
| 1885 | private static final int RIGHT_MARGIN = |
| 1886 | R.styleable.ViewGroup_MarginLayout_layout_marginRight; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1887 | private static final int BOTTOM_MARGIN = |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 1888 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1889 | |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 1890 | private static final int COLUMN = R.styleable.GridLayout_Layout_layout_column; |
| 1891 | private static final int COLUMN_SPAN = R.styleable.GridLayout_Layout_layout_columnSpan; |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 1892 | |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 1893 | private static final int ROW = R.styleable.GridLayout_Layout_layout_row; |
| 1894 | private static final int ROW_SPAN = R.styleable.GridLayout_Layout_layout_rowSpan; |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 1895 | |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 1896 | private static final int GRAVITY = R.styleable.GridLayout_Layout_layout_gravity; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1897 | |
| 1898 | // Instance variables |
| 1899 | |
| 1900 | /** |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1901 | * The spec that defines the vertical characteristics of the cell group |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1902 | * described by these layout parameters. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1903 | * If an assignment is made to this field after a measurement or layout operation |
| 1904 | * has already taken place, a call to |
| 1905 | * {@link ViewGroup#setLayoutParams(ViewGroup.LayoutParams)} |
| 1906 | * must be made to notify GridLayout of the change. GridLayout is normally able |
| 1907 | * to detect when code fails to observe this rule, issue a warning and take steps to |
| 1908 | * compensate for the omission. This facility is implemented on a best effort basis |
| 1909 | * and should not be relied upon in production code - so it is best to include the above |
| 1910 | * calls to remove the warnings as soon as it is practical. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1911 | */ |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1912 | public Spec rowSpec = Spec.UNDEFINED; |
| 1913 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1914 | /** |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1915 | * The spec that defines the horizontal characteristics of the cell group |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1916 | * described by these layout parameters. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 1917 | * If an assignment is made to this field after a measurement or layout operation |
| 1918 | * has already taken place, a call to |
| 1919 | * {@link ViewGroup#setLayoutParams(ViewGroup.LayoutParams)} |
| 1920 | * must be made to notify GridLayout of the change. GridLayout is normally able |
| 1921 | * to detect when code fails to observe this rule, issue a warning and take steps to |
| 1922 | * compensate for the omission. This facility is implemented on a best effort basis |
| 1923 | * and should not be relied upon in production code - so it is best to include the above |
| 1924 | * calls to remove the warnings as soon as it is practical. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1925 | */ |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1926 | public Spec columnSpec = Spec.UNDEFINED; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1927 | |
| 1928 | // Constructors |
| 1929 | |
| 1930 | private LayoutParams( |
| 1931 | int width, int height, |
| 1932 | int left, int top, int right, int bottom, |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1933 | Spec rowSpec, Spec columnSpec) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1934 | super(width, height); |
| 1935 | setMargins(left, top, right, bottom); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1936 | this.rowSpec = rowSpec; |
| 1937 | this.columnSpec = columnSpec; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /** |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1941 | * Constructs a new LayoutParams instance for this <code>rowSpec</code> |
| 1942 | * and <code>columnSpec</code>. All other fields are initialized with |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1943 | * default values as defined in {@link LayoutParams}. |
| 1944 | * |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1945 | * @param rowSpec the rowSpec |
| 1946 | * @param columnSpec the columnSpec |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1947 | */ |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1948 | public LayoutParams(Spec rowSpec, Spec columnSpec) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1949 | this(DEFAULT_WIDTH, DEFAULT_HEIGHT, |
| 1950 | DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 1951 | rowSpec, columnSpec); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * Constructs a new LayoutParams with default values as defined in {@link LayoutParams}. |
| 1956 | */ |
| 1957 | public LayoutParams() { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 1958 | this(Spec.UNDEFINED, Spec.UNDEFINED); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | // Copying constructors |
| 1962 | |
| 1963 | /** |
| 1964 | * {@inheritDoc} |
| 1965 | */ |
| 1966 | public LayoutParams(ViewGroup.LayoutParams params) { |
| 1967 | super(params); |
| 1968 | } |
| 1969 | |
| 1970 | /** |
| 1971 | * {@inheritDoc} |
| 1972 | */ |
| 1973 | public LayoutParams(MarginLayoutParams params) { |
| 1974 | super(params); |
| 1975 | } |
| 1976 | |
| 1977 | /** |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 1978 | * Copy constructor. Clones the width, height, margin values, row spec, |
| 1979 | * and column spec of the source. |
| 1980 | * |
| 1981 | * @param source The layout params to copy from. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1982 | */ |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 1983 | public LayoutParams(LayoutParams source) { |
| 1984 | super(source); |
| 1985 | |
| 1986 | this.rowSpec = source.rowSpec; |
| 1987 | this.columnSpec = source.columnSpec; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | // AttributeSet constructors |
| 1991 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 1992 | /** |
| 1993 | * {@inheritDoc} |
| 1994 | * |
| 1995 | * Values not defined in the attribute set take the default values |
| 1996 | * defined in {@link LayoutParams}. |
| 1997 | */ |
| 1998 | public LayoutParams(Context context, AttributeSet attrs) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 1999 | super(context, attrs); |
| 2000 | reInitSuper(context, attrs); |
| 2001 | init(context, attrs); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | // Implementation |
| 2005 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2006 | // Reinitialise the margins using a different default policy than MarginLayoutParams. |
| 2007 | // Here we use the value UNDEFINED (as distinct from zero) to represent the undefined state |
| 2008 | // so that a layout manager default can be accessed post set up. We need this as, at the |
| 2009 | // point of installation, we do not know how many rows/cols there are and therefore |
| 2010 | // which elements are positioned next to the container's trailing edges. We need to |
| 2011 | // know this as margins around the container's boundary should have different |
| 2012 | // defaults to those between peers. |
| 2013 | |
| 2014 | // This method could be parametrized and moved into MarginLayout. |
| 2015 | private void reInitSuper(Context context, AttributeSet attrs) { |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 2016 | TypedArray a = |
| 2017 | context.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2018 | try { |
| 2019 | int margin = a.getDimensionPixelSize(MARGIN, DEFAULT_MARGIN); |
| 2020 | |
| 2021 | this.leftMargin = a.getDimensionPixelSize(LEFT_MARGIN, margin); |
| 2022 | this.topMargin = a.getDimensionPixelSize(TOP_MARGIN, margin); |
| 2023 | this.rightMargin = a.getDimensionPixelSize(RIGHT_MARGIN, margin); |
| 2024 | this.bottomMargin = a.getDimensionPixelSize(BOTTOM_MARGIN, margin); |
| 2025 | } finally { |
| 2026 | a.recycle(); |
| 2027 | } |
| 2028 | } |
| 2029 | |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2030 | private void init(Context context, AttributeSet attrs) { |
Philip Milne | b0ce49b | 2011-07-15 15:39:07 -0700 | [diff] [blame] | 2031 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridLayout_Layout); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2032 | try { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2033 | int gravity = a.getInt(GRAVITY, Gravity.NO_GRAVITY); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2034 | |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 2035 | int column = a.getInt(COLUMN, DEFAULT_COLUMN); |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2036 | int colSpan = a.getInt(COLUMN_SPAN, DEFAULT_SPAN_SIZE); |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2037 | this.columnSpec = spec(column, colSpan, getAlignment(gravity, true)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2038 | |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 2039 | int row = a.getInt(ROW, DEFAULT_ROW); |
| 2040 | int rowSpan = a.getInt(ROW_SPAN, DEFAULT_SPAN_SIZE); |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2041 | this.rowSpec = spec(row, rowSpan, getAlignment(gravity, false)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2042 | } finally { |
| 2043 | a.recycle(); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /** |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2048 | * Describes how the child views are positioned. Default is {@code LEFT | BASELINE}. |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2049 | * See {@link Gravity}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2050 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2051 | * @param gravity the new gravity value |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2052 | * |
| 2053 | * @attr ref android.R.styleable#GridLayout_Layout_layout_gravity |
| 2054 | */ |
| 2055 | public void setGravity(int gravity) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2056 | rowSpec = rowSpec.copyWriteAlignment(getAlignment(gravity, false)); |
| 2057 | columnSpec = columnSpec.copyWriteAlignment(getAlignment(gravity, true)); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | @Override |
| 2061 | protected void setBaseAttributes(TypedArray attributes, int widthAttr, int heightAttr) { |
| 2062 | this.width = attributes.getLayoutDimension(widthAttr, DEFAULT_WIDTH); |
| 2063 | this.height = attributes.getLayoutDimension(heightAttr, DEFAULT_HEIGHT); |
| 2064 | } |
| 2065 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2066 | final void setRowSpecSpan(Interval span) { |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2067 | rowSpec = rowSpec.copyWriteSpan(span); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2070 | final void setColumnSpecSpan(Interval span) { |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2071 | columnSpec = columnSpec.copyWriteSpan(span); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2072 | } |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 2073 | |
| 2074 | @Override |
| 2075 | public boolean equals(Object o) { |
| 2076 | if (this == o) return true; |
| 2077 | if (o == null || getClass() != o.getClass()) return false; |
| 2078 | |
| 2079 | LayoutParams that = (LayoutParams) o; |
| 2080 | |
| 2081 | if (!columnSpec.equals(that.columnSpec)) return false; |
| 2082 | if (!rowSpec.equals(that.rowSpec)) return false; |
| 2083 | |
| 2084 | return true; |
| 2085 | } |
| 2086 | |
| 2087 | @Override |
| 2088 | public int hashCode() { |
| 2089 | int result = rowSpec.hashCode(); |
| 2090 | result = 31 * result + columnSpec.hashCode(); |
| 2091 | return result; |
| 2092 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2095 | /* |
| 2096 | In place of a HashMap from span to Int, use an array of key/value pairs - stored in Arcs. |
| 2097 | Add the mutables completesCycle flag to avoid creating another hash table for detecting cycles. |
| 2098 | */ |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2099 | final static class Arc { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2100 | public final Interval span; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2101 | public final MutableInt value; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2102 | public boolean valid = true; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2103 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2104 | public Arc(Interval span, MutableInt value) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2105 | this.span = span; |
| 2106 | this.value = value; |
| 2107 | } |
| 2108 | |
| 2109 | @Override |
| 2110 | public String toString() { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2111 | return span + " " + (!valid ? "+>" : "->") + " " + value; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | // A mutable Integer - used to avoid heap allocation during the layout operation |
| 2116 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2117 | final static class MutableInt { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2118 | public int value; |
| 2119 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2120 | public MutableInt() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2121 | reset(); |
| 2122 | } |
| 2123 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2124 | public MutableInt(int value) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2125 | this.value = value; |
| 2126 | } |
| 2127 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2128 | public void reset() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2129 | value = Integer.MIN_VALUE; |
| 2130 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2131 | |
| 2132 | @Override |
| 2133 | public String toString() { |
| 2134 | return Integer.toString(value); |
| 2135 | } |
| 2136 | } |
| 2137 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2138 | final static class Assoc<K, V> extends ArrayList<Pair<K, V>> { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2139 | private final Class<K> keyType; |
| 2140 | private final Class<V> valueType; |
| 2141 | |
| 2142 | private Assoc(Class<K> keyType, Class<V> valueType) { |
| 2143 | this.keyType = keyType; |
| 2144 | this.valueType = valueType; |
| 2145 | } |
| 2146 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2147 | public static <K, V> Assoc<K, V> of(Class<K> keyType, Class<V> valueType) { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2148 | return new Assoc<K, V>(keyType, valueType); |
| 2149 | } |
| 2150 | |
| 2151 | public void put(K key, V value) { |
| 2152 | add(Pair.create(key, value)); |
| 2153 | } |
| 2154 | |
| 2155 | @SuppressWarnings(value = "unchecked") |
| 2156 | public PackedMap<K, V> pack() { |
| 2157 | int N = size(); |
| 2158 | K[] keys = (K[]) Array.newInstance(keyType, N); |
| 2159 | V[] values = (V[]) Array.newInstance(valueType, N); |
| 2160 | for (int i = 0; i < N; i++) { |
| 2161 | keys[i] = get(i).first; |
| 2162 | values[i] = get(i).second; |
| 2163 | } |
| 2164 | return new PackedMap<K, V>(keys, values); |
| 2165 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2166 | } |
| 2167 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2168 | /* |
| 2169 | This data structure is used in place of a Map where we have an index that refers to the order |
| 2170 | in which each key/value pairs were added to the map. In this case we store keys and values |
| 2171 | in arrays of a length that is equal to the number of unique keys. We also maintain an |
| 2172 | array of indexes from insertion order to the compacted arrays of keys and values. |
| 2173 | |
| 2174 | Note that behavior differs from that of a LinkedHashMap in that repeated entries |
| 2175 | *do* get added multiples times. So the length of index is equals to the number of |
| 2176 | items added. |
| 2177 | |
| 2178 | This is useful in the GridLayout class where we can rely on the order of children not |
| 2179 | changing during layout - to use integer-based lookup for our internal structures |
| 2180 | rather than using (and storing) an implementation of Map<Key, ?>. |
| 2181 | */ |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2182 | @SuppressWarnings(value = "unchecked") |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2183 | final static class PackedMap<K, V> { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2184 | public final int[] index; |
| 2185 | public final K[] keys; |
| 2186 | public final V[] values; |
| 2187 | |
| 2188 | private PackedMap(K[] keys, V[] values) { |
| 2189 | this.index = createIndex(keys); |
| 2190 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2191 | this.keys = compact(keys, index); |
| 2192 | this.values = compact(values, index); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2193 | } |
| 2194 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2195 | public V getValue(int i) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2196 | return values[index[i]]; |
| 2197 | } |
| 2198 | |
| 2199 | private static <K> int[] createIndex(K[] keys) { |
| 2200 | int size = keys.length; |
| 2201 | int[] result = new int[size]; |
| 2202 | |
| 2203 | Map<K, Integer> keyToIndex = new HashMap<K, Integer>(); |
| 2204 | for (int i = 0; i < size; i++) { |
| 2205 | K key = keys[i]; |
| 2206 | Integer index = keyToIndex.get(key); |
| 2207 | if (index == null) { |
| 2208 | index = keyToIndex.size(); |
| 2209 | keyToIndex.put(key, index); |
| 2210 | } |
| 2211 | result[i] = index; |
| 2212 | } |
| 2213 | return result; |
| 2214 | } |
| 2215 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2216 | /* |
| 2217 | Create a compact array of keys or values using the supplied index. |
| 2218 | */ |
| 2219 | private static <K> K[] compact(K[] a, int[] index) { |
| 2220 | int size = a.length; |
| 2221 | Class<?> componentType = a.getClass().getComponentType(); |
Philip Milne | 51f17d5 | 2011-06-13 10:44:49 -0700 | [diff] [blame] | 2222 | K[] result = (K[]) Array.newInstance(componentType, max2(index, -1) + 1); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2223 | |
| 2224 | // this overwrite duplicates, retaining the last equivalent entry |
| 2225 | for (int i = 0; i < size; i++) { |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2226 | result[index[i]] = a[i]; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2227 | } |
| 2228 | return result; |
| 2229 | } |
| 2230 | } |
| 2231 | |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2232 | /* |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2233 | For each group (with a given alignment) we need to store the amount of space required |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2234 | before the alignment point and the amount of space required after it. One side of this |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2235 | calculation is always 0 for START and END alignments but we don't make use of this. |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2236 | For CENTER and BASELINE alignments both sides are needed and in the BASELINE case no |
| 2237 | simple optimisations are possible. |
| 2238 | |
| 2239 | The general algorithm therefore is to create a Map (actually a PackedMap) from |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2240 | group to Bounds and to loop through all Views in the group taking the maximum |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2241 | of the values for each View. |
| 2242 | */ |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2243 | static class Bounds { |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2244 | public int before; |
| 2245 | public int after; |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2246 | public int flexibility; // we're flexible iff all included specs are flexible |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2247 | |
| 2248 | private Bounds() { |
| 2249 | reset(); |
| 2250 | } |
| 2251 | |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2252 | protected void reset() { |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2253 | before = Integer.MIN_VALUE; |
| 2254 | after = Integer.MIN_VALUE; |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2255 | flexibility = CAN_STRETCH; // from the above, we're flexible when empty |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2256 | } |
| 2257 | |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2258 | protected void include(int before, int after) { |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2259 | this.before = max(this.before, before); |
| 2260 | this.after = max(this.after, after); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2263 | protected int size(boolean min) { |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 2264 | if (!min) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2265 | if (canStretch(flexibility)) { |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 2266 | return MAX_SIZE; |
| 2267 | } |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2268 | } |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2269 | return before + after; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 2272 | protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean horizontal) { |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2273 | return before - a.getAlignmentValue(c, size, gl.getLayoutMode()); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | protected final void include(GridLayout gl, View c, Spec spec, Axis axis) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2277 | this.flexibility &= spec.getFlexibility(); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 2278 | boolean horizontal = axis.horizontal; |
| 2279 | int size = gl.getMeasurementIncludingMargin(c, horizontal); |
| 2280 | Alignment alignment = gl.getAlignment(spec.alignment, horizontal); |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 2281 | // todo test this works correctly when the returned value is UNDEFINED |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2282 | int before = alignment.getAlignmentValue(c, size, gl.getLayoutMode()); |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2283 | include(before, size - before); |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2286 | @Override |
| 2287 | public String toString() { |
| 2288 | return "Bounds{" + |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2289 | "before=" + before + |
| 2290 | ", after=" + after + |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2291 | '}'; |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | /** |
| 2296 | * An Interval represents a contiguous range of values that lie between |
| 2297 | * the interval's {@link #min} and {@link #max} values. |
| 2298 | * <p> |
| 2299 | * Intervals are immutable so may be passed as values and used as keys in hash tables. |
| 2300 | * It is not necessary to have multiple instances of Intervals which have the same |
| 2301 | * {@link #min} and {@link #max} values. |
| 2302 | * <p> |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2303 | * Intervals are often written as {@code [min, max]} and represent the set of values |
| 2304 | * {@code x} such that {@code min <= x < max}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2305 | */ |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2306 | final static class Interval { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2307 | /** |
| 2308 | * The minimum value. |
| 2309 | */ |
| 2310 | public final int min; |
Philip Milne | aa616f3 | 2011-05-27 18:38:01 -0700 | [diff] [blame] | 2311 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2312 | /** |
| 2313 | * The maximum value. |
| 2314 | */ |
| 2315 | public final int max; |
| 2316 | |
| 2317 | /** |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2318 | * Construct a new Interval, {@code interval}, where: |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2319 | * <ul> |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2320 | * <li> {@code interval.min = min} </li> |
| 2321 | * <li> {@code interval.max = max} </li> |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2322 | * </ul> |
| 2323 | * |
| 2324 | * @param min the minimum value. |
| 2325 | * @param max the maximum value. |
| 2326 | */ |
| 2327 | public Interval(int min, int max) { |
| 2328 | this.min = min; |
| 2329 | this.max = max; |
| 2330 | } |
| 2331 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2332 | int size() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2333 | return max - min; |
| 2334 | } |
| 2335 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2336 | Interval inverse() { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2337 | return new Interval(max, min); |
| 2338 | } |
| 2339 | |
| 2340 | /** |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2341 | * Returns {@code true} if the {@link #getClass class}, |
| 2342 | * {@link #min} and {@link #max} properties of this Interval and the |
| 2343 | * supplied parameter are pairwise equal; {@code false} otherwise. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2344 | * |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2345 | * @param that the object to compare this interval with |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2346 | * |
| 2347 | * @return {@code true} if the specified object is equal to this |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2348 | * {@code Interval}, {@code false} otherwise. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2349 | */ |
| 2350 | @Override |
| 2351 | public boolean equals(Object that) { |
| 2352 | if (this == that) { |
| 2353 | return true; |
| 2354 | } |
| 2355 | if (that == null || getClass() != that.getClass()) { |
| 2356 | return false; |
| 2357 | } |
| 2358 | |
| 2359 | Interval interval = (Interval) that; |
| 2360 | |
| 2361 | if (max != interval.max) { |
| 2362 | return false; |
| 2363 | } |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 2364 | //noinspection RedundantIfStatement |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2365 | if (min != interval.min) { |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
| 2369 | return true; |
| 2370 | } |
| 2371 | |
| 2372 | @Override |
| 2373 | public int hashCode() { |
| 2374 | int result = min; |
| 2375 | result = 31 * result + max; |
| 2376 | return result; |
| 2377 | } |
| 2378 | |
| 2379 | @Override |
| 2380 | public String toString() { |
| 2381 | return "[" + min + ", " + max + "]"; |
| 2382 | } |
| 2383 | } |
| 2384 | |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2385 | /** |
| 2386 | * A Spec defines the horizontal or vertical characteristics of a group of |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 2387 | * cells. Each spec. defines the <em>grid indices</em> and <em>alignment</em> |
| 2388 | * along the appropriate axis. |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2389 | * <p> |
| 2390 | * The <em>grid indices</em> are the leading and trailing edges of this cell group. |
| 2391 | * See {@link GridLayout} for a description of the conventions used by GridLayout |
| 2392 | * for grid indices. |
| 2393 | * <p> |
| 2394 | * The <em>alignment</em> property specifies how cells should be aligned in this group. |
| 2395 | * For row groups, this specifies the vertical alignment. |
| 2396 | * For column groups, this specifies the horizontal alignment. |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 2397 | * <p> |
| 2398 | * Use the following static methods to create specs: |
| 2399 | * <ul> |
| 2400 | * <li>{@link #spec(int)}</li> |
| 2401 | * <li>{@link #spec(int, int)}</li> |
| 2402 | * <li>{@link #spec(int, Alignment)}</li> |
| 2403 | * <li>{@link #spec(int, int, Alignment)}</li> |
| 2404 | * </ul> |
| 2405 | * |
Philip Milne | 899d592 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2406 | */ |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2407 | public static class Spec { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2408 | static final Spec UNDEFINED = spec(GridLayout.UNDEFINED); |
| 2409 | |
| 2410 | final boolean startDefined; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2411 | final Interval span; |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2412 | final Alignment alignment; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2413 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2414 | private Spec(boolean startDefined, Interval span, Alignment alignment) { |
| 2415 | this.startDefined = startDefined; |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 2416 | this.span = span; |
| 2417 | this.alignment = alignment; |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2420 | private Spec(boolean startDefined, int start, int size, Alignment alignment) { |
| 2421 | this(startDefined, new Interval(start, start + size), alignment); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2424 | final Spec copyWriteSpan(Interval span) { |
| 2425 | return new Spec(startDefined, span, alignment); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2428 | final Spec copyWriteAlignment(Alignment alignment) { |
| 2429 | return new Spec(startDefined, span, alignment); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2432 | final int getFlexibility() { |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 2433 | return (alignment == UNDEFINED_ALIGNMENT) ? INFLEXIBLE : CAN_STRETCH; |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2436 | /** |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2437 | * Returns {@code true} if the {@code class}, {@code alignment} and {@code span} |
| 2438 | * properties of this Spec and the supplied parameter are pairwise equal, |
Philip Milne | 7fd9487 | 2011-06-07 20:14:17 -0700 | [diff] [blame] | 2439 | * {@code false} otherwise. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2440 | * |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2441 | * @param that the object to compare this spec with |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2442 | * |
| 2443 | * @return {@code true} if the specified object is equal to this |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2444 | * {@code Spec}; {@code false} otherwise |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2445 | */ |
| 2446 | @Override |
| 2447 | public boolean equals(Object that) { |
| 2448 | if (this == that) { |
| 2449 | return true; |
| 2450 | } |
| 2451 | if (that == null || getClass() != that.getClass()) { |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2455 | Spec spec = (Spec) that; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2456 | |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2457 | if (!alignment.equals(spec.alignment)) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2458 | return false; |
| 2459 | } |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 2460 | //noinspection RedundantIfStatement |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2461 | if (!span.equals(spec.span)) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2462 | return false; |
| 2463 | } |
| 2464 | |
| 2465 | return true; |
| 2466 | } |
| 2467 | |
| 2468 | @Override |
| 2469 | public int hashCode() { |
| 2470 | int result = span.hashCode(); |
| 2471 | result = 31 * result + alignment.hashCode(); |
| 2472 | return result; |
| 2473 | } |
| 2474 | } |
| 2475 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2476 | /** |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2477 | * Return a Spec, {@code spec}, where: |
| 2478 | * <ul> |
| 2479 | * <li> {@code spec.span = [start, start + size]} </li> |
| 2480 | * <li> {@code spec.alignment = alignment} </li> |
| 2481 | * </ul> |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2482 | * <p> |
| 2483 | * To leave the start index undefined, use the value {@link #UNDEFINED}. |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2484 | * |
| 2485 | * @param start the start |
| 2486 | * @param size the size |
| 2487 | * @param alignment the alignment |
| 2488 | */ |
| 2489 | public static Spec spec(int start, int size, Alignment alignment) { |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2490 | return new Spec(start != UNDEFINED, start, size, alignment); |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /** |
| 2494 | * Return a Spec, {@code spec}, where: |
| 2495 | * <ul> |
| 2496 | * <li> {@code spec.span = [start, start + 1]} </li> |
| 2497 | * <li> {@code spec.alignment = alignment} </li> |
| 2498 | * </ul> |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2499 | * <p> |
| 2500 | * To leave the start index undefined, use the value {@link #UNDEFINED}. |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2501 | * |
| 2502 | * @param start the start index |
| 2503 | * @param alignment the alignment |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2504 | * |
| 2505 | * @see #spec(int, int, Alignment) |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2506 | */ |
| 2507 | public static Spec spec(int start, Alignment alignment) { |
| 2508 | return spec(start, 1, alignment); |
| 2509 | } |
| 2510 | |
| 2511 | /** |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2512 | * Return a Spec, {@code spec}, where: |
| 2513 | * <ul> |
| 2514 | * <li> {@code spec.span = [start, start + size]} </li> |
| 2515 | * </ul> |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2516 | * <p> |
| 2517 | * To leave the start index undefined, use the value {@link #UNDEFINED}. |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2518 | * |
| 2519 | * @param start the start |
| 2520 | * @param size the size |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2521 | * |
| 2522 | * @see #spec(int, Alignment) |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2523 | */ |
| 2524 | public static Spec spec(int start, int size) { |
| 2525 | return spec(start, size, UNDEFINED_ALIGNMENT); |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * Return a Spec, {@code spec}, where: |
| 2530 | * <ul> |
| 2531 | * <li> {@code spec.span = [start, start + 1]} </li> |
| 2532 | * </ul> |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2533 | * <p> |
| 2534 | * To leave the start index undefined, use the value {@link #UNDEFINED}. |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2535 | * |
| 2536 | * @param start the start index |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2537 | * |
| 2538 | * @see #spec(int, int) |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2539 | */ |
| 2540 | public static Spec spec(int start) { |
| 2541 | return spec(start, 1); |
| 2542 | } |
| 2543 | |
| 2544 | /** |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2545 | * Alignments specify where a view should be placed within a cell group and |
| 2546 | * what size it should be. |
| 2547 | * <p> |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2548 | * The {@link LayoutParams} class contains a {@link LayoutParams#rowSpec rowSpec} |
| 2549 | * and a {@link LayoutParams#columnSpec columnSpec} each of which contains an |
| 2550 | * {@code alignment}. Overall placement of the view in the cell |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2551 | * group is specified by the two alignments which act along each axis independently. |
| 2552 | * <p> |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2553 | * The GridLayout class defines the most common alignments used in general layout: |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2554 | * {@link #TOP}, {@link #LEFT}, {@link #BOTTOM}, {@link #RIGHT}, {@link #START}, |
| 2555 | * {@link #END}, {@link #CENTER}, {@link #BASELINE} and {@link #FILL}. |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2556 | */ |
| 2557 | /* |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2558 | * An Alignment implementation must define {@link #getAlignmentValue(View, int, int)}, |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2559 | * to return the appropriate value for the type of alignment being defined. |
| 2560 | * The enclosing algorithms position the children |
Philip Milne | 1e54825 | 2011-06-16 19:02:33 -0700 | [diff] [blame] | 2561 | * so that the locations defined by the alignment values |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2562 | * are the same for all of the views in a group. |
| 2563 | * <p> |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2564 | */ |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2565 | public static abstract class Alignment { |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2566 | Alignment() { |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2569 | abstract int getGravityOffset(View view, int cellDelta); |
| 2570 | |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2571 | /** |
| 2572 | * Returns an alignment value. In the case of vertical alignments the value |
| 2573 | * returned should indicate the distance from the top of the view to the |
| 2574 | * alignment location. |
| 2575 | * For horizontal alignments measurement is made from the left edge of the component. |
| 2576 | * |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2577 | * @param view the view to which this alignment should be applied |
| 2578 | * @param viewSize the measured size of the view |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2579 | * @param mode the basis of alignment: CLIP or OPTICAL |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 2580 | * @return the alignment value |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2581 | */ |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2582 | abstract int getAlignmentValue(View view, int viewSize, int mode); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2583 | |
| 2584 | /** |
| 2585 | * Returns the size of the view specified by this alignment. |
| 2586 | * In the case of vertical alignments this method should return a height; for |
| 2587 | * horizontal alignments this method should return the width. |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2588 | * <p> |
| 2589 | * The default implementation returns {@code viewSize}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2590 | * |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2591 | * @param view the view to which this alignment should be applied |
| 2592 | * @param viewSize the measured size of the view |
| 2593 | * @param cellSize the size of the cell into which this view will be placed |
Philip Milne | b3a8c54 | 2011-06-20 16:02:59 -0700 | [diff] [blame] | 2594 | * @return the aligned size |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2595 | */ |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2596 | int getSizeInCell(View view, int viewSize, int cellSize) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2597 | return viewSize; |
| 2598 | } |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2599 | |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2600 | Bounds getBounds() { |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2601 | return new Bounds(); |
| 2602 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2603 | } |
| 2604 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2605 | static final Alignment UNDEFINED_ALIGNMENT = new Alignment() { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2606 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2607 | int getGravityOffset(View view, int cellDelta) { |
| 2608 | return UNDEFINED; |
| 2609 | } |
| 2610 | |
| 2611 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2612 | public int getAlignmentValue(View view, int viewSize, int mode) { |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2613 | return UNDEFINED; |
| 2614 | } |
| 2615 | }; |
| 2616 | |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2617 | /** |
| 2618 | * Indicates that a view should be aligned with the <em>start</em> |
| 2619 | * edges of the other views in its cell group. |
| 2620 | */ |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2621 | private static final Alignment LEADING = new Alignment() { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2622 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2623 | int getGravityOffset(View view, int cellDelta) { |
| 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2628 | public int getAlignmentValue(View view, int viewSize, int mode) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2629 | return 0; |
| 2630 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2631 | }; |
| 2632 | |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2633 | /** |
| 2634 | * Indicates that a view should be aligned with the <em>end</em> |
| 2635 | * edges of the other views in its cell group. |
| 2636 | */ |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2637 | private static final Alignment TRAILING = new Alignment() { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2638 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2639 | int getGravityOffset(View view, int cellDelta) { |
| 2640 | return cellDelta; |
| 2641 | } |
| 2642 | |
| 2643 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2644 | public int getAlignmentValue(View view, int viewSize, int mode) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2645 | return viewSize; |
| 2646 | } |
| 2647 | }; |
| 2648 | |
| 2649 | /** |
| 2650 | * Indicates that a view should be aligned with the <em>top</em> |
| 2651 | * edges of the other views in its cell group. |
| 2652 | */ |
| 2653 | public static final Alignment TOP = LEADING; |
| 2654 | |
| 2655 | /** |
| 2656 | * Indicates that a view should be aligned with the <em>bottom</em> |
| 2657 | * edges of the other views in its cell group. |
| 2658 | */ |
| 2659 | public static final Alignment BOTTOM = TRAILING; |
| 2660 | |
| 2661 | /** |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2662 | * Indicates that a view should be aligned with the <em>start</em> |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2663 | * edges of the other views in its cell group. |
| 2664 | */ |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2665 | public static final Alignment START = LEADING; |
| 2666 | |
| 2667 | /** |
| 2668 | * Indicates that a view should be aligned with the <em>end</em> |
| 2669 | * edges of the other views in its cell group. |
| 2670 | */ |
| 2671 | public static final Alignment END = TRAILING; |
| 2672 | |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2673 | private static Alignment createSwitchingAlignment(final Alignment ltr, final Alignment rtl) { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2674 | return new Alignment() { |
| 2675 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2676 | int getGravityOffset(View view, int cellDelta) { |
| 2677 | return (!view.isLayoutRtl() ? ltr : rtl).getGravityOffset(view, cellDelta); |
| 2678 | } |
| 2679 | |
| 2680 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2681 | public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2682 | return (!view.isLayoutRtl() ? ltr : rtl).getAlignmentValue(view, viewSize, mode); |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2683 | } |
| 2684 | }; |
| 2685 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2686 | |
| 2687 | /** |
| 2688 | * Indicates that a view should be aligned with the <em>left</em> |
| 2689 | * edges of the other views in its cell group. |
| 2690 | */ |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2691 | public static final Alignment LEFT = createSwitchingAlignment(START, END); |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2692 | |
| 2693 | /** |
| 2694 | * Indicates that a view should be aligned with the <em>right</em> |
| 2695 | * edges of the other views in its cell group. |
| 2696 | */ |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2697 | public static final Alignment RIGHT = createSwitchingAlignment(END, START); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2698 | |
| 2699 | /** |
| 2700 | * Indicates that a view should be <em>centered</em> with the other views in its cell group. |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2701 | * This constant may be used in both {@link LayoutParams#rowSpec rowSpecs} and {@link |
| 2702 | * LayoutParams#columnSpec columnSpecs}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2703 | */ |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2704 | public static final Alignment CENTER = new Alignment() { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2705 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2706 | int getGravityOffset(View view, int cellDelta) { |
| 2707 | return cellDelta >> 1; |
| 2708 | } |
| 2709 | |
| 2710 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2711 | public int getAlignmentValue(View view, int viewSize, int mode) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2712 | return viewSize >> 1; |
| 2713 | } |
| 2714 | }; |
| 2715 | |
| 2716 | /** |
| 2717 | * Indicates that a view should be aligned with the <em>baselines</em> |
| 2718 | * of the other views in its cell group. |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2719 | * This constant may only be used as an alignment in {@link LayoutParams#rowSpec rowSpecs}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2720 | * |
| 2721 | * @see View#getBaseline() |
| 2722 | */ |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2723 | public static final Alignment BASELINE = new Alignment() { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2724 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2725 | int getGravityOffset(View view, int cellDelta) { |
| 2726 | return 0; // baseline gravity is top |
| 2727 | } |
| 2728 | |
| 2729 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2730 | public int getAlignmentValue(View view, int viewSize, int mode) { |
Philip Milne | a841644 | 2013-02-25 10:49:39 -0800 | [diff] [blame] | 2731 | if (view.getVisibility() == GONE) { |
| 2732 | return 0; |
| 2733 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2734 | int baseline = view.getBaseline(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2735 | return baseline == -1 ? UNDEFINED : baseline; |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | @Override |
| 2739 | public Bounds getBounds() { |
| 2740 | return new Bounds() { |
| 2741 | /* |
| 2742 | In a baseline aligned row in which some components define a baseline |
| 2743 | and some don't, we need a third variable to properly account for all |
| 2744 | the sizes. This tracks the maximum size of all the components - |
| 2745 | including those that don't define a baseline. |
| 2746 | */ |
| 2747 | private int size; |
| 2748 | |
| 2749 | @Override |
| 2750 | protected void reset() { |
| 2751 | super.reset(); |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2752 | size = Integer.MIN_VALUE; |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | @Override |
| 2756 | protected void include(int before, int after) { |
| 2757 | super.include(before, after); |
| 2758 | size = max(size, before + after); |
| 2759 | } |
| 2760 | |
| 2761 | @Override |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2762 | protected int size(boolean min) { |
| 2763 | return max(super.size(min), size); |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | @Override |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 2767 | protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean hrz) { |
| 2768 | return max(0, super.getOffset(gl, c, a, size, hrz)); |
Philip Milne | a1f7b10 | 2011-06-23 11:10:13 -0700 | [diff] [blame] | 2769 | } |
| 2770 | }; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2771 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2772 | }; |
| 2773 | |
| 2774 | /** |
| 2775 | * Indicates that a view should expanded to fit the boundaries of its cell group. |
Philip Milne | 93cd6a6 | 2011-07-12 14:49:45 -0700 | [diff] [blame] | 2776 | * This constant may be used in both {@link LayoutParams#rowSpec rowSpecs} and |
| 2777 | * {@link LayoutParams#columnSpec columnSpecs}. |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2778 | */ |
| 2779 | public static final Alignment FILL = new Alignment() { |
Fabrice Di Meglio | 47d248e | 2012-02-08 17:57:48 -0800 | [diff] [blame] | 2780 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2781 | int getGravityOffset(View view, int cellDelta) { |
| 2782 | return 0; |
| 2783 | } |
| 2784 | |
| 2785 | @Override |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2786 | public int getAlignmentValue(View view, int viewSize, int mode) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2787 | return UNDEFINED; |
| 2788 | } |
| 2789 | |
Philip Milne | c9885f6 | 2011-06-15 17:07:35 -0700 | [diff] [blame] | 2790 | @Override |
Philip Milne | 6216e87 | 2012-02-16 17:15:50 -0800 | [diff] [blame] | 2791 | public int getSizeInCell(View view, int viewSize, int cellSize) { |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 2792 | return cellSize; |
| 2793 | } |
| 2794 | }; |
Philip Milne | 48b5524 | 2011-06-29 11:09:45 -0700 | [diff] [blame] | 2795 | |
Philip Milne | f6679c8 | 2011-09-18 11:36:57 -0700 | [diff] [blame] | 2796 | static boolean canStretch(int flexibility) { |
Philip Milne | 5d1a984 | 2011-07-07 11:47:08 -0700 | [diff] [blame] | 2797 | return (flexibility & CAN_STRETCH) != 0; |
| 2798 | } |
| 2799 | |
Philip Milne | 5125e21 | 2011-07-21 11:39:37 -0700 | [diff] [blame] | 2800 | private static final int INFLEXIBLE = 0; |
Philip Milne | 4c8cf4c | 2011-08-03 11:50:50 -0700 | [diff] [blame] | 2801 | private static final int CAN_STRETCH = 2; |
Jim Miller | 452eec3 | 2011-06-16 18:32:44 -0700 | [diff] [blame] | 2802 | } |