@@ -67,8 +67,10 @@ public class GridLayout extends Layout{
67
67
private boolean fillLastRow ;
68
68
private int portraitRows ;
69
69
private int portraitColumns ;
70
+
70
71
private int landscapeRows = -1 ;
71
72
private int landscapeColumns = -1 ;
73
+
72
74
/**
73
75
* When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This
74
76
* makes animating a grid layout component MUCH easier.
@@ -207,68 +209,55 @@ private int autoSizeCols(Container parent, int width, boolean landscapeMode) {
207
209
/**
208
210
* {@inheritDoc}
209
211
*/
210
- @ Override
211
212
public void layoutContainer (Container parent ) {
212
- Style s = parent .getStyle ();
213
- int width = parent .getLayoutWidth () - parent .getSideGap () - s .getHorizontalPadding ();
214
- int height = parent .getLayoutHeight () - parent .getBottomGap () - s .getVerticalPadding ();
213
+ Style s = parent .getStyle ();
214
+ int width = parent .getLayoutWidth () - parent .getSideGap () - s .getHorizontalPadding ();
215
+ int height = parent .getLayoutHeight () - parent .getBottomGap () - s .getVerticalPadding ();
215
216
int numOfcomponents = parent .getComponentCount ();
216
217
217
218
boolean landscapeMode = isLandscapeMode ();
218
219
autoSizeCols (parent , width , landscapeMode );
219
- //-----------------------------------------------------
220
- //Checking the hidden components to recalculate rows.
221
- //-----------------------------------------------------
222
- int totalComponentCountVisible = numOfcomponents ;
223
- for (int i = 0 ; i < numOfcomponents ; i ++) {
224
- Component cmp = parent .getComponentAt (i );
225
- if (hideZeroSized && cmp .isHidden ()) {
226
- totalComponentCountVisible --;
227
- }
220
+
221
+ int rows , columns ;
222
+ if (landscapeMode ) {
223
+ rows = landscapeRows ;
224
+ columns = landscapeColumns ;
225
+ } else {
226
+ rows = portraitRows ;
227
+ columns = portraitColumns ;
228
228
}
229
- //--------------------------
230
- //Calculating dinamic rows
231
- // Solution to issue : https://github.com/codenameone/CodenameOne/issues/3692
232
- // Date: 12-04-23
233
- //--------------------------
234
- CalculateDinamicRowsLayout rc = new CalculateDinamicRowsLayout (totalComponentCountVisible , (landscapeMode ? landscapeColumns : portraitColumns ));
235
- int rows = rc .getRows ();
236
- int columns = rc .getColumns ();
237
- //--------------------------
229
+
238
230
int x = s .getPaddingLeft (parent .isRTL ());
239
231
int y = s .getPaddingTop ();
240
232
241
233
boolean rtl = parent .isRTL ();
242
234
if (rtl ) {
243
- x += parent .getSideGap ();
235
+ x += parent .getSideGap ();
244
236
}
245
237
int localColumns = columns ;
246
238
int cmpWidth = width / columns ;
247
- int cmpHeight = cmpHeight = height / rows ;
248
-
249
- int row = 0 ;
239
+ int cmpHeight ;
240
+ if (numOfcomponents > rows * columns ) {
241
+ // actual rows number
242
+ cmpHeight = height / (numOfcomponents / columns + (numOfcomponents % columns == 0 ? 0 : 1 ));
243
+ } else {
244
+ cmpHeight = height / rows ;
245
+ }
246
+ int row = 0 ;
247
+
250
248
int offset = 0 ;
251
249
for (int iter = 0 ; iter < numOfcomponents ; iter ++){
252
- Component cmp = parent .getComponentAt (iter );
253
- Style cmpStyle = cmp .getStyle ();
254
- int marginLeft = cmpStyle .getMarginLeft (parent .isRTL ());
255
- int marginTop = cmpStyle .getMarginTop ();
256
- int marginRight = cmpStyle .getMarginRight (parent .isRTL ());
257
- int marginBottom = cmpStyle .getMarginBottom ();
258
-
250
+ Component cmp = parent .getComponentAt (iter );
251
+ Style cmpStyle = cmp .getStyle ();
252
+ int marginLeft = cmpStyle .getMarginLeft (parent .isRTL ());
253
+ int marginTop = cmpStyle .getMarginTop ();
259
254
if (hideZeroSized ) {
260
255
if (cmp .isHidden ()) {
261
256
continue ;
262
257
}
263
258
}
264
- //---------------------------------------------------
265
- //Setting component size
266
- //---------------------------------------------------
267
- cmp .setWidth (cmpWidth - marginLeft - marginRight );
268
- cmp .setHeight (cmpHeight - marginTop - marginBottom );
269
- //---------------------------------------------------
270
- //Setting component position
271
- //---------------------------------------------------
259
+ cmp .setWidth (cmpWidth - marginLeft - cmpStyle .getMarginRight (parent .isRTL ()));
260
+ cmp .setHeight (cmpHeight - marginTop - cmpStyle .getMarginBottom ());
272
261
if (rtl ) {
273
262
cmp .setX (x + (localColumns - 1 - (offset % localColumns )) * cmpWidth + marginLeft );
274
263
} else {
@@ -277,6 +266,7 @@ public void layoutContainer(Container parent) {
277
266
cmp .setY (y + row * cmpHeight + marginTop );
278
267
if ((offset + 1 ) % columns == 0 ){
279
268
row ++;
269
+
280
270
// check if we need to recalculate component widths
281
271
if (fillLastRow && row == rows - 1 ) {
282
272
localColumns = numOfcomponents % columns ;
@@ -302,38 +292,39 @@ public Dimension getPreferredSize(Container parent) {
302
292
for (int i =0 ; i < numOfcomponents ; i ++){
303
293
Component cmp = parent .getComponentAt (i );
304
294
if (hideZeroSized && cmp .isHidden ()) {
295
+ totalComponentCount --;
305
296
} else {
306
297
width = Math .max (width , cmp .getPreferredW () + cmp .getStyle ().getMarginLeftNoRTL () + cmp .getStyle ().getMarginRightNoRTL ());
307
298
height = Math .max (height , cmp .getPreferredH () + cmp .getStyle ().getMarginTop () + cmp .getStyle ().getMarginBottom ());
308
299
}
309
300
}
310
- //-----------------------------------------------------
311
301
312
302
boolean landscapeMode = isLandscapeMode ();
313
303
autoSizeCols (parent , parent .getWidth (), landscapeMode );
314
- //--------------------------
315
- //Calculating dinamic rows
316
- // Solution to issue : https://github.com/codenameone/CodenameOne/issues/3692
317
- // Date: 12-04-23
318
- //--------------------------
319
- CalculateDinamicRowsLayout rc = new CalculateDinamicRowsLayout ( totalComponentCountVisible , ( landscapeMode ? landscapeColumns : portraitColumns )) ;
320
- int rows = rc . getRows () ;
321
- int columns = rc . getColumns ();
322
- //--------------------------
304
+ int rows , columns ;
305
+ if ( landscapeMode ) {
306
+ rows = landscapeRows ;
307
+ columns = landscapeColumns ;
308
+ } else {
309
+ rows = portraitRows ;
310
+ columns = portraitColumns ;
311
+ }
312
+
323
313
if (columns > 1 ){
324
314
width = width *columns ;
325
315
}
326
- totalComponentCount --;
327
- if (rows > 1 ){
328
316
317
+ if (rows > 1 ){
318
+ if (totalComponentCount >rows *columns ){ //if there are more components than planned
319
+ height = height * (totalComponentCount /columns + (totalComponentCount %columns == 0 ? 0 : 1 ));
329
320
}else {
330
321
height = height *rows ;
331
322
}
332
323
}
333
324
334
325
Style s = parent .getStyle ();
335
- if ( totalComponentCount > rows * columns ){ //if there are more components than planned
336
- height = height * ( totalComponentCount / columns + ( totalComponentCount % columns == 0 ? 0 : 1 ));
326
+ return new Dimension ( width + s . getHorizontalPadding (),
327
+ height + s . getVerticalPadding ( ));
337
328
}
338
329
339
330
/**
@@ -425,38 +416,4 @@ public boolean isHideZeroSized() {
425
416
public void setHideZeroSized (boolean hideZeroSized ) {
426
417
this .hideZeroSized = hideZeroSized ;
427
418
}
428
-
429
- /**
430
- * used to calculate the dinamic rows
431
- */
432
- public class CalculateDinamicRowsLayout {
433
- int rows = 1 ;
434
- int columns = 1 ;
435
- /**
436
- *
437
- * @param qtyComponents quantity of components in layout
438
- * @param maxColums max columns
439
- */
440
- //-------------------------------
441
- public CalculateDinamicRowsLayout (int qtyComponents , int maxColums ) {
442
- if (qtyComponents > maxColums ) {
443
- rows = qtyComponents / maxColums ;
444
- if (!(qtyComponents % maxColums == 0 )) {
445
- rows ++;
446
- }
447
- }
448
- columns = maxColums ;
449
- }
450
- //-------------------------------
451
- public int getRows () {
452
- return rows ;
453
- }
454
- //-------------------------------
455
- public int getColumns () {
456
- return columns ;
457
- }
458
- //-------------------------------
459
- }//endClass
460
- }
461
- return new Dimension (width + s .getHorizontalPadding (),
462
- height + s .getVerticalPadding ());
419
+ }
0 commit comments