summaryrefslogtreecommitdiff
path: root/core/java/android/widget/GridLayout.java
diff options
context:
space:
mode:
authorYigit Boyar <yboyar@google.com>2015-03-05 13:59:56 -0800
committerYigit Boyar <yboyar@google.com>2015-03-05 22:01:14 +0000
commit6dafd87fb4174447018b044bc67818d54fab57d8 (patch)
tree67c0a4880db80e8f209b15cb245861d717748a24 /core/java/android/widget/GridLayout.java
parentcd4ed96d10d49cb182e66aa1735f2de7596707ee (diff)
Fix some weight bugs in grid layout
This CL also changes how GONE views are considered. Previously, they would still participate in weight calculations. Now, they are ignored. Bug: 19546781 Sub bugs: Bug: 19571249 Bug: 19571653 Bug: 19571319 Bug: 19571841 Change-Id: I7344f757d3a76be98bb92660f07d2a0cafa9ac12
Diffstat (limited to 'core/java/android/widget/GridLayout.java')
-rw-r--r--core/java/android/widget/GridLayout.java44
1 files changed, 30 insertions, 14 deletions
diff --git a/core/java/android/widget/GridLayout.java b/core/java/android/widget/GridLayout.java
index e8fe1912e405..4b2384a37e66 100644
--- a/core/java/android/widget/GridLayout.java
+++ b/core/java/android/widget/GridLayout.java
@@ -141,7 +141,9 @@ import static java.lang.Math.min;
* view was alone in a column, that column would itself collapse to zero width if and only if
* no gravity was defined on the view. If gravity was defined, then the gone-marked
* view has no effect on the layout and the container should be laid out as if the view
- * had never been added to it.
+ * had never been added to it. GONE views are taken to have zero weight during excess space
+ * distribution.
+ * <p>
* These statements apply equally to rows as well as columns, and to groups of rows or columns.
*
* <p>
@@ -1015,7 +1017,7 @@ public class GridLayout extends ViewGroup {
} else {
boolean horizontal = (mOrientation == HORIZONTAL);
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec;
- if (spec.alignment == FILL) {
+ if (spec.getAbsoluteAlignment(horizontal) == FILL) {
Interval span = spec.span;
Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis;
int[] locations = axis.getLocations();
@@ -1091,11 +1093,6 @@ public class GridLayout extends ViewGroup {
invalidateValues();
}
- final Alignment getAlignment(Alignment alignment, boolean horizontal) {
- return (alignment != UNDEFINED_ALIGNMENT) ? alignment :
- (horizontal ? START : BASELINE);
- }
-
// Layout container
/**
@@ -1150,8 +1147,8 @@ public class GridLayout extends ViewGroup {
int pWidth = getMeasurement(c, true);
int pHeight = getMeasurement(c, false);
- Alignment hAlign = getAlignment(columnSpec.alignment, true);
- Alignment vAlign = getAlignment(rowSpec.alignment, false);
+ Alignment hAlign = columnSpec.getAbsoluteAlignment(true);
+ Alignment vAlign = rowSpec.getAbsoluteAlignment(false);
Bounds boundsX = mHorizontalAxis.getGroupBounds().getValue(i);
Bounds boundsY = mVerticalAxis.getGroupBounds().getValue(i);
@@ -1297,7 +1294,7 @@ public class GridLayout extends ViewGroup {
// we must include views that are GONE here, see introductory javadoc
LayoutParams lp = getLayoutParams(c);
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec;
- Bounds bounds = getAlignment(spec.alignment, horizontal).getBounds();
+ Bounds bounds = spec.getAbsoluteAlignment(horizontal).getBounds();
assoc.put(spec, bounds);
}
return assoc.pack();
@@ -1703,7 +1700,11 @@ public class GridLayout extends ViewGroup {
private boolean computeHasWeights() {
for (int i = 0, N = getChildCount(); i < N; i++) {
- LayoutParams lp = getLayoutParams(getChildAt(i));
+ final View child = getChildAt(i);
+ if (child.getVisibility() == View.GONE) {
+ continue;
+ }
+ LayoutParams lp = getLayoutParams(child);
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec;
if (spec.weight != 0) {
return true;
@@ -1743,7 +1744,10 @@ public class GridLayout extends ViewGroup {
private void shareOutDelta(int totalDelta, float totalWeight) {
Arrays.fill(deltas, 0);
for (int i = 0, N = getChildCount(); i < N; i++) {
- View c = getChildAt(i);
+ final View c = getChildAt(i);
+ if (c.getVisibility() == View.GONE) {
+ continue;
+ }
LayoutParams lp = getLayoutParams(c);
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec;
float weight = spec.weight;
@@ -1796,6 +1800,9 @@ public class GridLayout extends ViewGroup {
float totalWeight = 0f;
for (int i = 0, N = getChildCount(); i < N; i++) {
View c = getChildAt(i);
+ if (c.getVisibility() == View.GONE) {
+ continue;
+ }
LayoutParams lp = getLayoutParams(c);
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec;
totalWeight += spec.weight;
@@ -2011,7 +2018,6 @@ public class GridLayout extends ViewGroup {
R.styleable.ViewGroup_MarginLayout_layout_marginRight;
private static final int BOTTOM_MARGIN =
R.styleable.ViewGroup_MarginLayout_layout_marginBottom;
-
private static final int COLUMN = R.styleable.GridLayout_Layout_layout_column;
private static final int COLUMN_SPAN = R.styleable.GridLayout_Layout_layout_columnSpan;
private static final int COLUMN_WEIGHT = R.styleable.GridLayout_Layout_layout_columnWeight;
@@ -2405,7 +2411,7 @@ public class GridLayout extends ViewGroup {
protected final void include(GridLayout gl, View c, Spec spec, Axis axis, int size) {
this.flexibility &= spec.getFlexibility();
boolean horizontal = axis.horizontal;
- Alignment alignment = gl.getAlignment(spec.alignment, horizontal);
+ Alignment alignment = spec.getAbsoluteAlignment(axis.horizontal);
// todo test this works correctly when the returned value is UNDEFINED
int before = alignment.getAlignmentValue(c, size, gl.getLayoutMode());
include(before, size - before);
@@ -2556,6 +2562,16 @@ public class GridLayout extends ViewGroup {
this(startDefined, new Interval(start, start + size), alignment, weight);
}
+ public Alignment getAbsoluteAlignment(boolean horizontal) {
+ if (alignment != UNDEFINED_ALIGNMENT) {
+ return alignment;
+ }
+ if (weight == 0f) {
+ return horizontal ? START : BASELINE;
+ }
+ return FILL;
+ }
+
final Spec copyWriteSpan(Interval span) {
return new Spec(startDefined, span, alignment, weight);
}