Merge pull request #15 from NativeScript/hhristov/grid-layout-fix

Fixed grid-layout distribution of free space when grid is not stretch…
This commit is contained in:
Hristo Hristov
2016-02-10 16:24:04 +02:00

View File

@@ -13,7 +13,6 @@ import android.view.View.MeasureSpec;
/** /**
* @author hhristov * @author hhristov
*
*/ */
public class GridLayout extends LayoutBase { public class GridLayout extends LayoutBase {
@@ -198,7 +197,7 @@ public class GridLayout extends LayoutBase {
} }
private void updateMeasureSpecs(View child, MeasureSpecs measureSpec) { private void updateMeasureSpecs(View child, MeasureSpecs measureSpec) {
CommonLayoutParams lp = (CommonLayoutParams)child.getLayoutParams(); CommonLayoutParams lp = (CommonLayoutParams) child.getLayoutParams();
int columnIndex = this.getColumnIndex(lp); int columnIndex = this.getColumnIndex(lp);
ItemSpec column = this.getColumnSpec(lp); ItemSpec column = this.getColumnSpec(lp);
int rowIndex = this.getRowIndex(lp); int rowIndex = this.getRowIndex(lp);
@@ -445,7 +444,7 @@ class MeasureSpecs {
} }
class ItemGroup { class ItemGroup {
float length = 0; int length = 0;
int measuredCount = 0; int measuredCount = 0;
ItemSpec rowOrColumn; ItemSpec rowOrColumn;
ArrayList<MeasureSpecs> children = new ArrayList<MeasureSpecs>(); ArrayList<MeasureSpecs> children = new ArrayList<MeasureSpecs>();
@@ -506,12 +505,15 @@ class MeasureHelper {
private boolean infinityWidth = false; private boolean infinityWidth = false;
private boolean infinityHeight = false; private boolean infinityHeight = false;
private float minColumnStarValue;
private float maxColumnStarValue;
private float minRowStarValue;
private float maxRowStarValue;
int measuredWidth; int measuredWidth;
int measuredHeight; int measuredHeight;
private float columnStarValue;
private float rowStarValue;
private boolean fakeRowAdded = false; private boolean fakeRowAdded = false;
private boolean fakeColumnAdded = false; private boolean fakeColumnAdded = false;
@@ -519,15 +521,11 @@ class MeasureHelper {
this.grid = grid; this.grid = grid;
} }
public boolean getColumnsFixed() {
return this.columnStarValue >= 0;
}
public void setInfinityWidth(boolean value) { public void setInfinityWidth(boolean value) {
if (this.infinityWidth != value) { if (this.infinityWidth != value) {
this.infinityWidth = value; this.infinityWidth = value;
for(int i = 0, size = this.columns.size(); i < size; i++) { for (int i = 0, size = this.columns.size(); i < size; i++) {
ItemGroup columnGroup = this.columns.get(i); ItemGroup columnGroup = this.columns.get(i);
columnGroup.setIsLengthInfinity(value); columnGroup.setIsLengthInfinity(value);
} }
@@ -538,7 +536,7 @@ class MeasureHelper {
if (this.infinityHeight != value) { if (this.infinityHeight != value) {
this.infinityHeight = value; this.infinityHeight = value;
for(int i = 0, size = this.rows.size(); i < size; i++) { for (int i = 0, size = this.rows.size(); i < size; i++) {
ItemGroup rowGroup = this.rows.get(i); ItemGroup rowGroup = this.rows.get(i);
rowGroup.setIsLengthInfinity(value); rowGroup.setIsLengthInfinity(value);
} }
@@ -552,11 +550,9 @@ class MeasureHelper {
ItemGroup columnGroup = this.columns.get(i); ItemGroup columnGroup = this.columns.get(i);
if (columnGroup.getIsAuto()) { if (columnGroup.getIsAuto()) {
measureSpec.autoColumnsCount++; measureSpec.autoColumnsCount++;
} } else if (columnGroup.getIsStar()) {
else if (columnGroup.getIsStar()) {
measureSpec.starColumnsCount += columnGroup.rowOrColumn.getValue(); measureSpec.starColumnsCount += columnGroup.rowOrColumn.getValue();
} } else if (columnGroup.getIsAbsolute()) {
else if (columnGroup.getIsAbsolute()) {
measureSpec.pixelWidth += columnGroup.rowOrColumn.getValue(); measureSpec.pixelWidth += columnGroup.rowOrColumn.getValue();
} }
} }
@@ -577,11 +573,9 @@ class MeasureHelper {
ItemGroup rowGroup = this.rows.get(i); ItemGroup rowGroup = this.rows.get(i);
if (rowGroup.getIsAuto()) { if (rowGroup.getIsAuto()) {
measureSpec.autoRowsCount++; measureSpec.autoRowsCount++;
} } else if (rowGroup.getIsStar()) {
else if (rowGroup.getIsStar()) {
measureSpec.starRowsCount += rowGroup.rowOrColumn.getValue(); measureSpec.starRowsCount += rowGroup.rowOrColumn.getValue();
} } else if (rowGroup.getIsAbsolute()) {
else if (rowGroup.getIsAbsolute()) {
measureSpec.pixelHeight += rowGroup.rowOrColumn.getValue(); measureSpec.pixelHeight += rowGroup.rowOrColumn.getValue();
} }
} }
@@ -611,7 +605,7 @@ class MeasureHelper {
} }
private static void initList(ArrayList<ItemGroup> list) { private static void initList(ArrayList<ItemGroup> list) {
for(int i = 0, size = list.size(); i < size; i++) { for (int i = 0, size = list.size(); i < size; i++) {
ItemGroup item = list.get(i); ItemGroup item = list.get(i);
item.init(); item.init();
} }
@@ -637,8 +631,7 @@ class MeasureHelper {
this.fakeColumnAdded = true; this.fakeColumnAdded = true;
singleColumnGroup.setIsLengthInfinity(this.infinityWidth); singleColumnGroup.setIsLengthInfinity(this.infinityWidth);
this.columns.add(singleColumnGroup); this.columns.add(singleColumnGroup);
} } else if (cols > 1 && this.fakeColumnAdded) {
else if (cols > 1 && this.fakeColumnAdded) {
this.columns.remove(0); this.columns.remove(0);
this.fakeColumnAdded = false; this.fakeColumnAdded = false;
} }
@@ -646,8 +639,10 @@ class MeasureHelper {
initList(this.rows); initList(this.rows);
initList(this.columns); initList(this.columns);
this.columnStarValue = -1; this.minColumnStarValue = -1;
this.rowStarValue = -1; this.minRowStarValue = -1;
this.maxColumnStarValue = -1;
this.maxRowStarValue = -1;
} }
private void itemMeasured(MeasureSpecs measureSpec, boolean isFakeMeasure) { private void itemMeasured(MeasureSpecs measureSpec, boolean isFakeMeasure) {
@@ -679,56 +674,60 @@ class MeasureHelper {
} }
private void fixColumns() { private void fixColumns() {
float currentColumnWidth = 0; int currentColumnWidth = 0;
int columnStarCount = 0; int columnStarCount = 0;
int columnCount = this.columns.size(); int columnCount = this.columns.size();
for(int i = 0; i < columnCount; i++) {
ItemGroup item = this.columns.get(i);
// Star columns are still zeros (not calculated).
currentColumnWidth += item.length;
if (item.rowOrColumn.getIsStar()) {
columnStarCount += item.rowOrColumn.getValue();
}
}
this.columnStarValue = columnStarCount > 0 ? (this.width - currentColumnWidth) / columnStarCount : 0;
if (this.stretchedHorizontally) {
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
ItemGroup item = this.columns.get(i); ItemGroup item = this.columns.get(i);
if (item.getIsStar()) { if (item.rowOrColumn.getIsStar()) {
item.length = item.rowOrColumn.getValue() * this.columnStarValue; columnStarCount += item.rowOrColumn.getValue();
} } else {
// Star columns are still zeros (not calculated).
currentColumnWidth += item.length;
} }
} }
float widthForStarColumns = Math.max(0, this.width - currentColumnWidth);
this.maxColumnStarValue = columnStarCount > 0 ? widthForStarColumns / columnStarCount : 0;
updateStarLength(this.columns, this.maxColumnStarValue);
} }
private void fixRows() { private void fixRows() {
float currentRowHeight = 0; int currentRowHeight = 0;
int rowStarCount = 0; int rowStarCount = 0;
int rowCount = this.rows.size(); int rowCount = this.rows.size();
for(int i = 0; i < rowCount; i++) {
ItemGroup item = this.rows.get(i);
// Star rows are still zeros (not calculated).
currentRowHeight += item.length;
if (item.rowOrColumn.getIsStar()) {
rowStarCount += item.rowOrColumn.getValue();
}
}
this.rowStarValue = rowStarCount > 0 ? (this.height - currentRowHeight) / rowStarCount : 0;
if(this.stretchedVertically) {
for (int i = 0; i < rowCount; i++) { for (int i = 0; i < rowCount; i++) {
ItemGroup item = this.rows.get(i); ItemGroup item = this.rows.get(i);
if (item.getIsStar()) { if (item.rowOrColumn.getIsStar()) {
item.length = item.rowOrColumn.getValue() * this.rowStarValue; rowStarCount += item.rowOrColumn.getValue();
} else {
// Star rows are still zeros (not calculated).
currentRowHeight += item.length;
} }
} }
float heightForStarRows = Math.max(0, this.height - currentRowHeight);
this.maxRowStarValue = rowStarCount > 0 ? heightForStarRows / rowStarCount : 0;
updateStarLength(this.rows, this.maxRowStarValue);
}
private static void updateStarLength(ArrayList<ItemGroup> list, float starValue) {
float offset = 0;
int roundedOffset = 0;
for (int i = 0, rowCount = list.size(); i < rowCount; i++) {
ItemGroup item = list.get(i);
if (item.getIsStar()) {
offset += item.rowOrColumn.getValue() * starValue;
float actualLength = offset - roundedOffset;
int roundedLength = Math.round(actualLength);
item.length = roundedLength;
roundedOffset += roundedLength;
}
} }
} }
@@ -764,7 +763,7 @@ class MeasureHelper {
private void measureNoStarColumnsFixedRows() { private void measureNoStarColumnsFixedRows() {
for (int i = 0, size = this.columns.size(); i < size; i++) { for (int i = 0, size = this.columns.size(); i < size; i++) {
ItemGroup columnGroup = this.columns.get(i); ItemGroup columnGroup = this.columns.get(i);
for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount ; j++) { for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount; j++) {
MeasureSpecs measureSpec = columnGroup.children.get(j); MeasureSpecs measureSpec = columnGroup.children.get(j);
if (measureSpec.starRowsCount > 0 && measureSpec.starColumnsCount == 0) { if (measureSpec.starRowsCount > 0 && measureSpec.starColumnsCount == 0) {
this.measureChildFixedRows(measureSpec); this.measureChildFixedRows(measureSpec);
@@ -776,7 +775,7 @@ class MeasureHelper {
private static boolean canFix(ArrayList<ItemGroup> list) { private static boolean canFix(ArrayList<ItemGroup> list) {
for (int i = 0, size = list.size(); i < size; i++) { for (int i = 0, size = list.size(); i < size; i++) {
ItemGroup item = list.get(i); ItemGroup item = list.get(i);
if(!item.getCanBeFixed()) { if (!item.getCanBeFixed()) {
return false; return false;
} }
} }
@@ -846,15 +845,13 @@ class MeasureHelper {
this.measureFixedColumnsNoStarRows(); this.measureFixedColumnsNoStarRows();
this.fixRows(); this.fixRows();
} } else if (fixColumns && !fixRows) {
else if (fixColumns && !fixRows) {
// Measure all elements that have star columns(already fixed) but no stars at the rows // Measure all elements that have star columns(already fixed) but no stars at the rows
this.measureFixedColumnsNoStarRows(); this.measureFixedColumnsNoStarRows();
// Then // Then
this.fixRows(); this.fixRows();
} } else if (!fixColumns && fixRows) {
else if (!fixColumns && fixRows) {
// Measure all elements that have star rows(already fixed) but no star at the columns // Measure all elements that have star rows(already fixed) but no star at the columns
this.measureNoStarColumnsFixedRows(); this.measureNoStarColumnsFixedRows();
@@ -874,6 +871,18 @@ class MeasureHelper {
} }
} }
// If we are not stretched and minColumnStarValue is less than maxColumnStarValue
// we need to reduce the length of star columns.
if (!this.stretchedHorizontally && this.minColumnStarValue != -1 && this.minColumnStarValue < this.maxColumnStarValue) {
updateStarLength(this.columns, this.minColumnStarValue);
}
// If we are not stretched and minRowStarValue is less than maxRowStarValue
// we need to reduce the height of star maxRowStarValue.
if (!this.stretchedVertically && this.minRowStarValue != -1 && this.minRowStarValue < this.maxRowStarValue) {
updateStarLength(this.rows, this.minRowStarValue);
}
this.measuredWidth = getMeasureLength(this.columns); this.measuredWidth = getMeasureLength(this.columns);
this.measuredHeight = getMeasureLength(this.rows); this.measuredHeight = getMeasureLength(this.rows);
} }
@@ -936,17 +945,13 @@ class MeasureHelper {
int rowIndex = measureSpec.getRowIndex(); int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan(); int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
int columnsWidth = 0; int measureWidth = 0;
int growSize = 0; int growSize = 0;
for (int i = columnIndex; i < columnSpanEnd; i++) { for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i); ItemGroup columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) { measureWidth += columnGroup.length;
columnsWidth += columnGroup.length;
} }
}
int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, this.stretchedHorizontally ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, this.stretchedHorizontally ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
int heightMeasureSpec = (measureSpec.autoRowsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelHeight, MeasureSpec.EXACTLY); int heightMeasureSpec = (measureSpec.autoRowsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelHeight, MeasureSpec.EXACTLY);
@@ -955,24 +960,7 @@ class MeasureHelper {
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(measureSpec.child); final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(measureSpec.child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(measureSpec.child); final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(measureSpec.child);
// Distribute width over star columns this.updateMinColumnStarValueIfNeeded(measureSpec, childMeasuredWidth);
if (!this.stretchedHorizontally) {
int remainingSpace = childMeasuredWidth;
for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i);
remainingSpace -= columnGroup.length;
}
if (remainingSpace > 0) {
growSize = remainingSpace / measureSpec.starColumnsCount;
for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i);
if (columnGroup.getIsStar()) {
columnGroup.length += growSize;
}
}
}
}
// Distribute height over auto rows // Distribute height over auto rows
if (measureSpec.autoRowsCount > 0) { if (measureSpec.autoRowsCount > 0) {
@@ -1002,16 +990,12 @@ class MeasureHelper {
int columnSpanEnd = columnIndex + measureSpec.getColumnSpan(); int columnSpanEnd = columnIndex + measureSpec.getColumnSpan();
int rowIndex = measureSpec.getRowIndex(); int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan(); int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
int rowsHeight = 0; int measureHeight = 0;
for (int i = rowIndex; i < rowSpanEnd; i++) { for (int i = rowIndex; i < rowSpanEnd; i++) {
ItemGroup rowGroup = this.rows.get(i); ItemGroup rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) { measureHeight += rowGroup.length;
rowsHeight += rowGroup.length;
} }
}
int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
int widthMeasureSpec = (measureSpec.autoColumnsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelWidth, MeasureSpec.EXACTLY); int widthMeasureSpec = (measureSpec.autoColumnsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, this.stretchedVertically ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, this.stretchedVertically ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
@@ -1036,7 +1020,6 @@ class MeasureHelper {
growSize = remainingSpace / measureSpec.autoColumnsCount; growSize = remainingSpace / measureSpec.autoColumnsCount;
for (int i = columnIndex; i < columnSpanEnd; i++) { for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i); ItemGroup columnGroup = this.columns.get(i);
if (columnGroup.getIsAuto()) { if (columnGroup.getIsAuto()) {
columnGroup.length += growSize; columnGroup.length += growSize;
} }
@@ -1044,25 +1027,7 @@ class MeasureHelper {
} }
} }
// Distribute height over star rows this.updateMinRowStarValueIfNeeded(measureSpec, childMeasuredHeight);
if (!this.stretchedVertically) {
remainingSpace = childMeasuredHeight;
for (int i = rowIndex; i < rowSpanEnd; i++) {
ItemGroup rowGroup = this.rows.get(i);
remainingSpace -= rowGroup.length;
}
if (remainingSpace > 0) {
growSize = remainingSpace / measureSpec.starRowsCount;
for (int i = rowIndex; i < rowSpanEnd; i++) {
ItemGroup rowGroup = this.rows.get(i);
if (rowGroup.getIsStar()) {
rowGroup.length += growSize;
}
}
}
}
this.itemMeasured(measureSpec, false); this.itemMeasured(measureSpec, false);
} }
@@ -1072,27 +1037,17 @@ class MeasureHelper {
int rowIndex = measureSpec.getRowIndex(); int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan(); int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
ItemGroup columnGroup; int measureWidth = 0;
ItemGroup rowGroup;
float columnsWidth = 0;
for (int i = columnIndex; i < columnSpanEnd; i++) { for (int i = columnIndex; i < columnSpanEnd; i++) {
columnGroup = this.columns.get(i); ItemGroup columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) { measureWidth += columnGroup.length;
columnsWidth += columnGroup.length;
}
} }
float rowsHeight = 0; int measureHeight = 0;
for (int i = rowIndex; i < rowSpanEnd; i++) { for (int i = rowIndex; i < rowSpanEnd; i++) {
rowGroup = this.rows.get(i); ItemGroup rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) { measureHeight += rowGroup.length;
rowsHeight += rowGroup.length;
} }
}
int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
// if (have stars) & (not stretch) - at most // if (have stars) & (not stretch) - at most
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth,
@@ -1105,45 +1060,46 @@ class MeasureHelper {
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(measureSpec.child); final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(measureSpec.child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(measureSpec.child); final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(measureSpec.child);
int remainingSpace = childMeasuredWidth; this.updateMinColumnStarValueIfNeeded(measureSpec, childMeasuredWidth);
int growSize = 0; this.updateMinRowStarValueIfNeeded(measureSpec, childMeasuredHeight);
if (!this.stretchedHorizontally) {
for (int i = columnIndex; i < columnSpanEnd; i++) {
columnGroup = this.columns.get(i);
remainingSpace -= columnGroup.length;
}
if (remainingSpace > 0) {
growSize = remainingSpace / measureSpec.starColumnsCount;
for (int i = columnIndex; i < columnSpanEnd; i++) {
columnGroup = this.columns.get(i);
if (columnGroup.getIsStar()) {
columnGroup.length += growSize;
}
}
}
}
remainingSpace = childMeasuredHeight;
if (!this.stretchedVertically) {
for (int i = rowIndex; i < rowSpanEnd; i++) {
rowGroup = this.rows.get(i);
remainingSpace -= rowGroup.length;
}
if (remainingSpace > 0) {
growSize = remainingSpace / measureSpec.starRowsCount;
for (int i = rowIndex; i < rowSpanEnd; i++) {
rowGroup = this.rows.get(i);
if (rowGroup.getIsStar()) {
rowGroup.length += growSize;
}
}
}
}
this.itemMeasured(measureSpec, false); this.itemMeasured(measureSpec, false);
} }
private void updateMinRowStarValueIfNeeded(MeasureSpecs measureSpec, int childMeasuredHeight) {
if (!this.stretchedVertically && measureSpec.starRowsCount > 0) {
int remainingSpace = childMeasuredHeight;
int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
for (int i = rowIndex; i < rowSpanEnd; i++) {
ItemGroup rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) {
remainingSpace -= rowGroup.length;
}
}
if (remainingSpace > 0) {
this.minRowStarValue = Math.max(remainingSpace / measureSpec.starRowsCount, this.minRowStarValue);
}
}
}
private void updateMinColumnStarValueIfNeeded(MeasureSpecs measureSpec, int childMeasuredWidth) {
// When not stretched star columns are not fixed so we may grow them here
// if there is an element that spans on multiple columns
if (!this.stretchedHorizontally && measureSpec.starColumnsCount > 0) {
int remainingSpace = childMeasuredWidth;
int columnIndex = measureSpec.getColumnIndex();
int columnSpanEnd = columnIndex + measureSpec.getColumnSpan();
for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) {
remainingSpace -= columnGroup.length;
}
}
if (remainingSpace > 0) {
this.minColumnStarValue = Math.max(remainingSpace / measureSpec.starColumnsCount, this.minColumnStarValue);
}
}
}
} }