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
*
*/
public class GridLayout extends LayoutBase {
@@ -445,7 +444,7 @@ class MeasureSpecs {
}
class ItemGroup {
float length = 0;
int length = 0;
int measuredCount = 0;
ItemSpec rowOrColumn;
ArrayList<MeasureSpecs> children = new ArrayList<MeasureSpecs>();
@@ -506,12 +505,15 @@ class MeasureHelper {
private boolean infinityWidth = false;
private boolean infinityHeight = false;
private float minColumnStarValue;
private float maxColumnStarValue;
private float minRowStarValue;
private float maxRowStarValue;
int measuredWidth;
int measuredHeight;
private float columnStarValue;
private float rowStarValue;
private boolean fakeRowAdded = false;
private boolean fakeColumnAdded = false;
@@ -519,10 +521,6 @@ class MeasureHelper {
this.grid = grid;
}
public boolean getColumnsFixed() {
return this.columnStarValue >= 0;
}
public void setInfinityWidth(boolean value) {
if (this.infinityWidth != value) {
this.infinityWidth = value;
@@ -552,11 +550,9 @@ class MeasureHelper {
ItemGroup columnGroup = this.columns.get(i);
if (columnGroup.getIsAuto()) {
measureSpec.autoColumnsCount++;
}
else if (columnGroup.getIsStar()) {
} else if (columnGroup.getIsStar()) {
measureSpec.starColumnsCount += columnGroup.rowOrColumn.getValue();
}
else if (columnGroup.getIsAbsolute()) {
} else if (columnGroup.getIsAbsolute()) {
measureSpec.pixelWidth += columnGroup.rowOrColumn.getValue();
}
}
@@ -577,11 +573,9 @@ class MeasureHelper {
ItemGroup rowGroup = this.rows.get(i);
if (rowGroup.getIsAuto()) {
measureSpec.autoRowsCount++;
}
else if (rowGroup.getIsStar()) {
} else if (rowGroup.getIsStar()) {
measureSpec.starRowsCount += rowGroup.rowOrColumn.getValue();
}
else if (rowGroup.getIsAbsolute()) {
} else if (rowGroup.getIsAbsolute()) {
measureSpec.pixelHeight += rowGroup.rowOrColumn.getValue();
}
}
@@ -637,8 +631,7 @@ class MeasureHelper {
this.fakeColumnAdded = true;
singleColumnGroup.setIsLengthInfinity(this.infinityWidth);
this.columns.add(singleColumnGroup);
}
else if (cols > 1 && this.fakeColumnAdded) {
} else if (cols > 1 && this.fakeColumnAdded) {
this.columns.remove(0);
this.fakeColumnAdded = false;
}
@@ -646,8 +639,10 @@ class MeasureHelper {
initList(this.rows);
initList(this.columns);
this.columnStarValue = -1;
this.rowStarValue = -1;
this.minColumnStarValue = -1;
this.minRowStarValue = -1;
this.maxColumnStarValue = -1;
this.maxRowStarValue = -1;
}
private void itemMeasured(MeasureSpecs measureSpec, boolean isFakeMeasure) {
@@ -679,55 +674,59 @@ class MeasureHelper {
}
private void fixColumns() {
float currentColumnWidth = 0;
int currentColumnWidth = 0;
int columnStarCount = 0;
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();
} else {
// Star columns are still zeros (not calculated).
currentColumnWidth += item.length;
}
}
this.columnStarValue = columnStarCount > 0 ? (this.width - currentColumnWidth) / columnStarCount : 0;
float widthForStarColumns = Math.max(0, this.width - currentColumnWidth);
this.maxColumnStarValue = columnStarCount > 0 ? widthForStarColumns / columnStarCount : 0;
if (this.stretchedHorizontally) {
for (int i = 0; i < columnCount; i++) {
ItemGroup item = this.columns.get(i);
if (item.getIsStar()) {
item.length = item.rowOrColumn.getValue() * this.columnStarValue;
}
}
}
updateStarLength(this.columns, this.maxColumnStarValue);
}
private void fixRows() {
float currentRowHeight = 0;
int currentRowHeight = 0;
int rowStarCount = 0;
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();
} else {
// Star rows are still zeros (not calculated).
currentRowHeight += item.length;
}
}
this.rowStarValue = rowStarCount > 0 ? (this.height - currentRowHeight) / rowStarCount : 0;
float heightForStarRows = Math.max(0, this.height - currentRowHeight);
this.maxRowStarValue = rowStarCount > 0 ? heightForStarRows / rowStarCount : 0;
if(this.stretchedVertically) {
for (int i = 0; i < rowCount; i++) {
ItemGroup item = this.rows.get(i);
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()) {
item.length = item.rowOrColumn.getValue() * this.rowStarValue;
}
offset += item.rowOrColumn.getValue() * starValue;
float actualLength = offset - roundedOffset;
int roundedLength = Math.round(actualLength);
item.length = roundedLength;
roundedOffset += roundedLength;
}
}
}
@@ -846,15 +845,13 @@ class MeasureHelper {
this.measureFixedColumnsNoStarRows();
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
this.measureFixedColumnsNoStarRows();
// Then
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
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.measuredHeight = getMeasureLength(this.rows);
}
@@ -936,17 +945,13 @@ class MeasureHelper {
int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
int columnsWidth = 0;
int measureWidth = 0;
int growSize = 0;
for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) {
columnsWidth += columnGroup.length;
measureWidth += columnGroup.length;
}
}
int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, this.stretchedHorizontally ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
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 childMeasuredHeight = CommonLayoutParams.getDesiredHeight(measureSpec.child);
// Distribute width over star columns
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;
}
}
}
}
this.updateMinColumnStarValueIfNeeded(measureSpec, childMeasuredWidth);
// Distribute height over auto rows
if (measureSpec.autoRowsCount > 0) {
@@ -1002,16 +990,12 @@ class MeasureHelper {
int columnSpanEnd = columnIndex + measureSpec.getColumnSpan();
int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
int rowsHeight = 0;
int measureHeight = 0;
for (int i = rowIndex; i < rowSpanEnd; i++) {
ItemGroup rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) {
rowsHeight += rowGroup.length;
measureHeight += rowGroup.length;
}
}
int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
int widthMeasureSpec = (measureSpec.autoColumnsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, this.stretchedVertically ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
@@ -1036,7 +1020,6 @@ class MeasureHelper {
growSize = remainingSpace / measureSpec.autoColumnsCount;
for (int i = columnIndex; i < columnSpanEnd; i++) {
ItemGroup columnGroup = this.columns.get(i);
if (columnGroup.getIsAuto()) {
columnGroup.length += growSize;
}
@@ -1044,25 +1027,7 @@ class MeasureHelper {
}
}
// Distribute height over star rows
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.updateMinRowStarValueIfNeeded(measureSpec, childMeasuredHeight);
this.itemMeasured(measureSpec, false);
}
@@ -1072,27 +1037,17 @@ class MeasureHelper {
int rowIndex = measureSpec.getRowIndex();
int rowSpanEnd = rowIndex + measureSpec.getRowSpan();
ItemGroup columnGroup;
ItemGroup rowGroup;
float columnsWidth = 0;
int measureWidth = 0;
for (int i = columnIndex; i < columnSpanEnd; i++) {
columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) {
columnsWidth += columnGroup.length;
}
ItemGroup columnGroup = this.columns.get(i);
measureWidth += columnGroup.length;
}
float rowsHeight = 0;
int measureHeight = 0;
for (int i = rowIndex; i < rowSpanEnd; i++) {
rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) {
rowsHeight += rowGroup.length;
ItemGroup rowGroup = this.rows.get(i);
measureHeight += 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
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth,
@@ -1105,45 +1060,46 @@ class MeasureHelper {
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(measureSpec.child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(measureSpec.child);
int remainingSpace = childMeasuredWidth;
int growSize = 0;
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.updateMinColumnStarValueIfNeeded(measureSpec, childMeasuredWidth);
this.updateMinRowStarValueIfNeeded(measureSpec, childMeasuredHeight);
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);
}
}
}
}