Skip to content

Commit 2527776

Browse files
author
Hristo Hristov
committed
Merge pull request #11 from NativeScript/hhristov/grid-layout-fix
Fixed grid layout alignments when width/height is specified.
2 parents 59f36fe + 994339e commit 2527776

2 files changed

Lines changed: 23 additions & 39 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tns-core-modules-widgets",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "Native widgets used in the NativeScript framework.",
55
"main": "index.js",
66
"scripts": {

‎widgets/src/main/java/org/nativescript/widgets/GridLayout.java‎

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
249249
boolean infinityWidth = widthMode == MeasureSpec.UNSPECIFIED;
250250
boolean infinityHeight = heightMode == MeasureSpec.UNSPECIFIED;
251251

252-
this.helper.width = width - horizontalPadding;
253-
this.helper.height = height - verticalPadding;
252+
this.helper.width = Math.max(0, width - horizontalPadding);
253+
this.helper.height = Math.max(0, height - verticalPadding);
254254

255255
int gravity = LayoutBase.getGravity(this);
256256
int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
257257
final int layoutDirection = this.getLayoutDirection();
258258
final int horizontalGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection) & Gravity.HORIZONTAL_GRAVITY_MASK;
259259

260-
this.helper.stretchedHorizontally = horizontalGravity == Gravity.FILL_HORIZONTAL && !infinityWidth;
261-
this.helper.stretchedVertically = verticalGravity == Gravity.FILL_VERTICAL && !infinityHeight;
260+
this.helper.stretchedHorizontally = widthMode == MeasureSpec.EXACTLY || (horizontalGravity == Gravity.FILL_HORIZONTAL && !infinityWidth);
261+
this.helper.stretchedVertically = heightMode == MeasureSpec.EXACTLY || (verticalGravity == Gravity.FILL_VERTICAL && !infinityHeight);
262262

263263
this.helper.setInfinityWidth(infinityWidth);
264264
this.helper.setInfinityHeight(infinityHeight);
@@ -527,8 +527,7 @@ public void setInfinityWidth(boolean value) {
527527
if (this.infinityWidth != value) {
528528
this.infinityWidth = value;
529529

530-
int size = this.columns.size();
531-
for(int i = 0; i < size; i++) {
530+
for(int i = 0, size = this.columns.size(); i < size; i++) {
532531
ItemGroup columnGroup = this.columns.get(i);
533532
columnGroup.setIsLengthInfinity(value);
534533
}
@@ -538,9 +537,8 @@ public void setInfinityWidth(boolean value) {
538537
public void setInfinityHeight(boolean value) {
539538
if (this.infinityHeight != value) {
540539
this.infinityHeight = value;
541-
542-
int size = this.rows.size();
543-
for(int i = 0; i < size; i++) {
540+
541+
for(int i = 0, size = this.rows.size(); i < size; i++) {
544542
ItemGroup rowGroup = this.rows.get(i);
545543
rowGroup.setIsLengthInfinity(value);
546544
}
@@ -603,20 +601,17 @@ else if (rowGroup.getIsAbsolute()) {
603601
}
604602

605603
public void clearMeasureSpecs() {
606-
int size = this.columns.size();
607-
for (int i = 0; i < size; i++) {
604+
for (int i = 0, size = this.columns.size(); i < size; i++) {
608605
this.columns.get(i).children.clear();
609606
}
610-
611-
size = this.rows.size();
612-
for (int i = 0; i < size; i++) {
607+
608+
for (int i = 0, size = this.rows.size(); i < size; i++) {
613609
this.rows.get(i).children.clear();
614610
}
615611
}
616612

617613
private static void initList(ArrayList<ItemGroup> list) {
618-
int size = list.size();
619-
for(int i = 0; i < size; i++) {
614+
for(int i = 0, size = list.size(); i < size; i++) {
620615
ItemGroup item = list.get(i);
621616
item.init();
622617
}
@@ -739,15 +734,13 @@ private void fixRows() {
739734

740735
private void fakeMeasure() {
741736
// Fake measure - measure all elements that have star rows and auto columns - with infinity Width and infinity Height
742-
int size = this.columns.size();
743-
for (int i = 0; i < size; i++) {
737+
for (int i = 0, size = this.columns.size(); i < size; i++) {
744738
ItemGroup columnGroup = this.columns.get(i);
745739
if (columnGroup.getAllMeasured()) {
746740
continue;
747741
}
748742

749-
int childrenCount = columnGroup.children.size();
750-
for (int j = 0; j < childrenCount; j++) {
743+
for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount; j++) {
751744
MeasureSpecs measureSpec = columnGroup.children.get(j);
752745
if (measureSpec.starRowsCount > 0 && measureSpec.autoColumnsCount > 0 && measureSpec.starColumnsCount == 0) {
753746
this.measureChild(measureSpec, true);
@@ -757,11 +750,9 @@ private void fakeMeasure() {
757750
}
758751

759752
private void measureFixedColumnsNoStarRows() {
760-
int size = this.columns.size();
761-
for (int i = 0; i < size; i++) {
753+
for (int i = 0, size = this.columns.size(); i < size; i++) {
762754
ItemGroup columnGroup = this.columns.get(i);
763-
int childrenCount = columnGroup.children.size();
764-
for (int j = 0; j < childrenCount; j++) {
755+
for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount; j++) {
765756
MeasureSpecs measureSpec = columnGroup.children.get(j);
766757
if (measureSpec.starColumnsCount > 0 && measureSpec.starRowsCount == 0) {
767758
this.measureChildFixedColumns(measureSpec);
@@ -771,11 +762,9 @@ private void measureFixedColumnsNoStarRows() {
771762
}
772763

773764
private void measureNoStarColumnsFixedRows() {
774-
int size = this.columns.size();
775-
for (int i = 0; i < size; i++) {
765+
for (int i = 0, size = this.columns.size(); i < size; i++) {
776766
ItemGroup columnGroup = this.columns.get(i);
777-
int childrenCount = columnGroup.children.size();
778-
for (int j = 0; j < childrenCount ; j++) {
767+
for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount ; j++) {
779768
MeasureSpecs measureSpec = columnGroup.children.get(j);
780769
if (measureSpec.starRowsCount > 0 && measureSpec.starColumnsCount == 0) {
781770
this.measureChildFixedRows(measureSpec);
@@ -785,8 +774,7 @@ private void measureNoStarColumnsFixedRows() {
785774
}
786775

787776
private static boolean canFix(ArrayList<ItemGroup> list) {
788-
int size = list.size();
789-
for (int i = 0; i < size; i++) {
777+
for (int i = 0, size = list.size(); i < size; i++) {
790778
ItemGroup item = list.get(i);
791779
if(!item.getCanBeFixed()) {
792780
return false;
@@ -798,8 +786,7 @@ private static boolean canFix(ArrayList<ItemGroup> list) {
798786

799787
private static int getMeasureLength(ArrayList<ItemGroup> list) {
800788
int result = 0;
801-
int size = list.size();
802-
for (int i = 0; i < size; i++) {
789+
for (int i = 0, size = list.size(); i < size; i++) {
803790
ItemGroup item = list.get(i);
804791
result += item.length;
805792
}
@@ -813,8 +800,7 @@ public void measure() {
813800
int size = this.columns.size();
814801
for (int i = 0; i < size; i++) {
815802
ItemGroup columnGroup = this.columns.get(i);
816-
int childrenCount = columnGroup.children.size();
817-
for (int j = 0; j < childrenCount; j++) {
803+
for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount; j++) {
818804
MeasureSpecs measureSpec = columnGroup.children.get(j);
819805
if (measureSpec.getIsStar() || measureSpec.getSpanned()) {
820806
continue;
@@ -827,8 +813,7 @@ public void measure() {
827813
// Measure auto & pixel columns and rows (with spans).
828814
for (int i = 0; i < size; i++) {
829815
ItemGroup columnGroup = this.columns.get(i);
830-
int childrenCount = columnGroup.children.size();
831-
for (int j = 0; j < childrenCount; j++) {
816+
for (int j = 0, childrenCount = columnGroup.children.size(); j < childrenCount; j++) {
832817
MeasureSpecs measureSpec = columnGroup.children.get(j);
833818
if (measureSpec.getIsStar() || !measureSpec.getSpanned()) {
834819
continue;
@@ -881,8 +866,7 @@ else if (!fixColumns && fixRows) {
881866
size = this.columns.size();
882867
for (int i = 0; i < size; i++) {
883868
ItemGroup columnGroup = this.columns.get(i);
884-
int childCount = columnGroup.children.size();
885-
for (int j = 0; j < childCount; j++) {
869+
for (int j = 0, childCount = columnGroup.children.size(); j < childCount; j++) {
886870
MeasureSpecs measureSpec = columnGroup.children.get(j);
887871
if (!measureSpec.measured) {
888872
this.measureChildFixedColumnsAndRows(measureSpec);

0 commit comments

Comments
 (0)