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