@@ -233,17 +233,32 @@ public void onAttachedToWindow() {
233
233
mContainer = (ConstraintLayout ) getParent ();
234
234
mConstraintSet .clone (mContainer );
235
235
236
- generateGrid ();
236
+ generateGrid (false );
237
237
}
238
238
239
239
/**
240
240
* generate the Grid form based on the input attributes
241
+ * @param isUpdate whether to update the existing grid (true) or create a new one (false)
241
242
* @return true if all the inputs are valid else false
242
243
*/
243
- private boolean generateGrid () {
244
+ private boolean generateGrid (boolean isUpdate ) {
245
+ if (mContainer == null || mConstraintSet == null ) {
246
+ return false ;
247
+ }
248
+
249
+ if (isUpdate ) {
250
+ for (int i = 0 ; i < mPositionMatrix .length ; i ++) {
251
+ for (int j = 0 ; j < mPositionMatrix [0 ].length ; j ++) {
252
+ mPositionMatrix [i ][j ] = true ;
253
+ }
254
+ }
255
+ mSpanIds .clear ();
256
+ }
257
+
258
+ mNextAvailableIndex = 0 ;
244
259
boolean isSuccess = true ;
245
260
246
- createGuidelines (mRows , mColumns );
261
+ createGuidelines (mRows , mColumns , isUpdate );
247
262
248
263
if (mStrSkips != null && !mStrSkips .trim ().isEmpty ()) {
249
264
HashMap <Integer , Pair <Integer , Integer >> mSkipMap = parseSpans (mStrSkips );
@@ -305,8 +320,9 @@ private float[] parseWeights(int size, String str) {
305
320
* create vertical and horizontal guidelines based on mRows and mColumns
306
321
* @param rows number of rows is required for grid
307
322
* @param columns number of columns is required for grid
323
+ * @param isUpdate whether to update existing guidelines (true) or create new ones (false)
308
324
*/
309
- private void createGuidelines (int rows , int columns ) {
325
+ private void createGuidelines (int rows , int columns , boolean isUpdate ) {
310
326
float [] rowWeights = parseWeights (rows , mStrRowWeights );
311
327
float [] columnWeights = parseWeights (columns , mStrColumnWeights );
312
328
@@ -316,11 +332,21 @@ private void createGuidelines(int rows, int columns) {
316
332
columns + 1 , columnWeights );
317
333
318
334
for (int i = 0 ; i < mHorizontalGuideLines .length ; i ++) {
335
+ if (isUpdate ) {
336
+ updateGuideLinePosition (mHorizontalGuideLines [i ], horizontalPositions [i ]);
337
+ continue ;
338
+ }
339
+
319
340
mHorizontalGuideLines [i ] = getNewGuideline (myContext ,
320
341
ConstraintLayout .LayoutParams .HORIZONTAL , horizontalPositions [i ]);
321
342
mContainer .addView (mHorizontalGuideLines [i ]);
322
343
}
323
344
for (int i = 0 ; i < mVerticalGuideLines .length ; i ++) {
345
+ if (isUpdate ) {
346
+ updateGuideLinePosition (mVerticalGuideLines [i ], verticalPositions [i ]);
347
+ continue ;
348
+ }
349
+
324
350
mVerticalGuideLines [i ] = getNewGuideline (myContext ,
325
351
ConstraintLayout .LayoutParams .VERTICAL , verticalPositions [i ]);
326
352
mContainer .addView (mVerticalGuideLines [i ]);
@@ -347,6 +373,13 @@ private Guideline getNewGuideline(Context context, int orientation, float positi
347
373
return guideline ;
348
374
}
349
375
376
+ private void updateGuideLinePosition (Guideline guideline , float position ) {
377
+ ConstraintLayout .LayoutParams params =
378
+ (ConstraintLayout .LayoutParams ) guideline .getLayoutParams ();
379
+ params .guidePercent = position ;
380
+ guideline .setLayoutParams (params );
381
+ }
382
+
350
383
/**
351
384
* Connect the view to the corresponding guidelines based on the input params
352
385
* @param viewId the Id of the view
@@ -604,8 +637,13 @@ public boolean setRows(int rows) {
604
637
return false ;
605
638
}
606
639
640
+ if (mRows == rows ) {
641
+ return true ;
642
+ }
643
+
607
644
mRows = rows ;
608
645
initVariables ();
646
+ generateGrid (false );
609
647
invalidate ();
610
648
return true ;
611
649
}
@@ -628,8 +666,13 @@ public boolean setColumns(int columns) {
628
666
return false ;
629
667
}
630
668
669
+ if (mColumns == columns ) {
670
+ return true ;
671
+ }
672
+
631
673
mColumns = columns ;
632
674
initVariables ();
675
+ generateGrid (false );
633
676
invalidate ();
634
677
return true ;
635
678
}
@@ -653,17 +696,17 @@ public boolean setOrientation(String orientation) {
653
696
return false ;
654
697
}
655
698
656
- if (orientation .equals (mOrientation )) {
699
+ if (mOrientation != null && mOrientation .equals (orientation )) {
657
700
return true ;
658
701
}
659
702
660
703
mOrientation = orientation ;
704
+ generateGrid (true );
661
705
invalidate ();
662
706
return true ;
663
707
664
708
}
665
709
666
-
667
710
/**
668
711
* get the string value of spans
669
712
* @return the string value of spans
@@ -681,7 +724,13 @@ public Boolean setSpans(String spans) {
681
724
if (!isSpansValid (spans )) {
682
725
return false ;
683
726
}
727
+
728
+ if (mStrSpans != null && mStrSpans .equals (spans )) {
729
+ return true ;
730
+ }
731
+
684
732
mStrSpans = spans ;
733
+ generateGrid (true );
685
734
invalidate ();
686
735
return true ;
687
736
}
@@ -703,7 +752,13 @@ public Boolean setSkips(String skips) {
703
752
if (!isSpansValid (skips )) {
704
753
return false ;
705
754
}
755
+
756
+ if (mStrSkips != null && mStrSkips .equals (skips )) {
757
+ return true ;
758
+ }
759
+
706
760
mStrSkips = skips ;
761
+ generateGrid (true );
707
762
invalidate ();
708
763
return true ;
709
764
}
@@ -719,14 +774,19 @@ public String getRowWeights() {
719
774
/**
720
775
* set new rowWeights value and also invoke invalidate
721
776
* @param rowWeights new rowWeights value
722
- * @return rue if it succeeds otherwise false
777
+ * @return true if it succeeds otherwise false
723
778
*/
724
779
public Boolean setRowWeights (String rowWeights ) {
725
780
if (!isWeightsValid (rowWeights )) {
726
781
return false ;
727
782
}
728
783
784
+ if (mStrRowWeights != null && mStrRowWeights .equals (rowWeights )) {
785
+ return true ;
786
+ }
787
+
729
788
mStrRowWeights = rowWeights ;
789
+ generateGrid (true );
730
790
invalidate ();
731
791
return true ;
732
792
}
@@ -742,14 +802,75 @@ public String getColumnWeights() {
742
802
/**
743
803
* set new columnWeights value and also invoke invalidate
744
804
* @param columnWeights new columnWeights value
745
- * @return rue if it succeeds otherwise false
805
+ * @return true if it succeeds otherwise false
746
806
*/
747
807
public Boolean setColumnWeights (String columnWeights ) {
748
808
if (!isWeightsValid (columnWeights )) {
749
809
return false ;
750
810
}
751
811
812
+ if (mStrColumnWeights != null && mStrColumnWeights .equals (columnWeights )) {
813
+ return true ;
814
+ }
815
+
752
816
mStrColumnWeights = columnWeights ;
817
+ generateGrid (true );
818
+ invalidate ();
819
+ return true ;
820
+ }
821
+
822
+ /**
823
+ * get the value of horizontalGaps
824
+ * @return the value of horizontalGaps
825
+ */
826
+ public int getHorizontalGaps () {
827
+ return mHorizontalGaps ;
828
+ }
829
+
830
+ /**
831
+ * set new horizontalGaps value and also invoke invalidate
832
+ * @param horizontalGaps new horizontalGaps value
833
+ * @return true if it succeeds otherwise false
834
+ */
835
+ public boolean setHorizontalGaps (int horizontalGaps ) {
836
+ if (horizontalGaps < 0 ) {
837
+ return false ;
838
+ }
839
+
840
+ if (mHorizontalGaps == horizontalGaps ) {
841
+ return true ;
842
+ }
843
+
844
+ mHorizontalGaps = horizontalGaps ;
845
+ generateGrid (true );
846
+ invalidate ();
847
+ return true ;
848
+ }
849
+
850
+ /**
851
+ * get the value of verticalGaps
852
+ * @return the value of verticalGaps
853
+ */
854
+ public int getVerticalGaps () {
855
+ return mVerticalGaps ;
856
+ }
857
+
858
+ /**
859
+ * set new verticalGaps value and also invoke invalidate
860
+ * @param verticalGaps new verticalGaps value
861
+ * @return true if it succeeds otherwise false
862
+ */
863
+ public boolean setVerticalGaps (int verticalGaps ) {
864
+ if (verticalGaps < 0 ) {
865
+ return false ;
866
+ }
867
+
868
+ if (mVerticalGaps == verticalGaps ) {
869
+ return true ;
870
+ }
871
+
872
+ mVerticalGaps = verticalGaps ;
873
+ generateGrid (true );
753
874
invalidate ();
754
875
return true ;
755
876
}
0 commit comments