From d44e11381d0ef6a25511a654aeb377f21bbecc28 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Fri, 5 Feb 2016 16:09:57 +0200 Subject: [PATCH] Fixed Issue #1232: WrapLayout crashes when itemWidth value is too high. --- .../org/nativescript/widgets/WrapLayout.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/widgets/src/main/java/org/nativescript/widgets/WrapLayout.java b/widgets/src/main/java/org/nativescript/widgets/WrapLayout.java index affa49fb9..ead440970 100644 --- a/widgets/src/main/java/org/nativescript/widgets/WrapLayout.java +++ b/widgets/src/main/java/org/nativescript/widgets/WrapLayout.java @@ -112,14 +112,15 @@ public class WrapLayout extends LayoutBase { CommonLayoutParams.measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec); final int childMeasuredWidth = this.getDesiredWidth(child); final int childMeasuredHeight = this.getDesiredHeight(child); + final boolean isFirst = this._lengths.size() <= rowOrColumn; if (isVertical) { if (childMeasuredHeight > remainingHeight) { rowOrColumn++; maxLength = Math.max(maxLength, measureHeight); measureHeight = childMeasuredHeight; - remainingWidth = availableHeight - childMeasuredHeight; - this._lengths.add(rowOrColumn, childMeasuredWidth); + remainingHeight = availableHeight - childMeasuredHeight; + this._lengths.add(isFirst ? rowOrColumn - 1 : rowOrColumn, childMeasuredWidth); } else { remainingHeight -= childMeasuredHeight; @@ -132,7 +133,7 @@ public class WrapLayout extends LayoutBase { maxLength = Math.max(maxLength, measureWidth); measureWidth = childMeasuredWidth; remainingWidth = availableWidth - childMeasuredWidth; - this._lengths.add(rowOrColumn, childMeasuredHeight); + this._lengths.add(isFirst ? rowOrColumn - 1 : rowOrColumn, childMeasuredHeight); } else { remainingWidth -= childMeasuredWidth; @@ -140,7 +141,7 @@ public class WrapLayout extends LayoutBase { } } - if(this._lengths.size() <= rowOrColumn) { + if(isFirst) { this._lengths.add(rowOrColumn, isVertical ? childMeasuredWidth : childMeasuredHeight); } else { @@ -200,34 +201,40 @@ public class WrapLayout extends LayoutBase { int length = this._lengths.get(rowOrColumn); if (isVertical) { childWidth = length; + final boolean isFirst = childTop == paddingTop; if (childTop + childHeight > childrenLength) { // Move to top. childTop = paddingTop; - // Move to right with current column width. - childLeft += length; + if (!isFirst){ + // Move to right with current column width. + childLeft += length; + } // Move to next column. rowOrColumn++; - // Take current column width. - childWidth = length = this._lengths.get(rowOrColumn); + // Take respective column width. + childWidth = this._lengths.get(isFirst ? rowOrColumn - 1 : rowOrColumn); } } else { childHeight = length; + final boolean isFirst = childLeft == paddingLeft; if (childLeft + childWidth > childrenLength) { // Move to left. childLeft = paddingLeft; - // Move to bottom with current row height. - childTop += length; + if (!isFirst) { + // Move to bottom with current row height. + childTop += length; + } - // Move to next column. + // Move to next row. rowOrColumn++; - // Take current row height. - childHeight = length = this._lengths.get(rowOrColumn); + // Take respective row height. + childHeight = this._lengths.get(isFirst ? rowOrColumn - 1 : rowOrColumn); } }