Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit ad7b832

Browse files
committed
[Grid] fix setter methods
1 parent 010ca5f commit ad7b832

File tree

4 files changed

+483
-16
lines changed

4 files changed

+483
-16
lines changed

constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/Grid.java

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,32 @@ public void onAttachedToWindow() {
233233
mContainer = (ConstraintLayout) getParent();
234234
mConstraintSet.clone(mContainer);
235235

236-
generateGrid();
236+
generateGrid(false);
237237
}
238238

239239
/**
240240
* 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)
241242
* @return true if all the inputs are valid else false
242243
*/
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;
244259
boolean isSuccess = true;
245260

246-
createGuidelines(mRows, mColumns);
261+
createGuidelines(mRows, mColumns, isUpdate);
247262

248263
if (mStrSkips != null && !mStrSkips.trim().isEmpty()) {
249264
HashMap<Integer, Pair<Integer, Integer>> mSkipMap = parseSpans(mStrSkips);
@@ -305,8 +320,9 @@ private float[] parseWeights(int size, String str) {
305320
* create vertical and horizontal guidelines based on mRows and mColumns
306321
* @param rows number of rows is required for grid
307322
* @param columns number of columns is required for grid
323+
* @param isUpdate whether to update existing guidelines (true) or create new ones (false)
308324
*/
309-
private void createGuidelines(int rows, int columns) {
325+
private void createGuidelines(int rows, int columns, boolean isUpdate) {
310326
float[] rowWeights = parseWeights(rows, mStrRowWeights);
311327
float[] columnWeights = parseWeights(columns, mStrColumnWeights);
312328

@@ -316,11 +332,21 @@ private void createGuidelines(int rows, int columns) {
316332
columns + 1, columnWeights);
317333

318334
for (int i = 0; i < mHorizontalGuideLines.length; i++) {
335+
if (isUpdate) {
336+
updateGuideLinePosition(mHorizontalGuideLines[i], horizontalPositions[i]);
337+
continue;
338+
}
339+
319340
mHorizontalGuideLines[i] = getNewGuideline(myContext,
320341
ConstraintLayout.LayoutParams.HORIZONTAL, horizontalPositions[i]);
321342
mContainer.addView(mHorizontalGuideLines[i]);
322343
}
323344
for (int i = 0; i < mVerticalGuideLines.length; i++) {
345+
if (isUpdate) {
346+
updateGuideLinePosition(mVerticalGuideLines[i], verticalPositions[i]);
347+
continue;
348+
}
349+
324350
mVerticalGuideLines[i] = getNewGuideline(myContext,
325351
ConstraintLayout.LayoutParams.VERTICAL, verticalPositions[i]);
326352
mContainer.addView(mVerticalGuideLines[i]);
@@ -347,6 +373,13 @@ private Guideline getNewGuideline(Context context, int orientation, float positi
347373
return guideline;
348374
}
349375

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+
350383
/**
351384
* Connect the view to the corresponding guidelines based on the input params
352385
* @param viewId the Id of the view
@@ -604,8 +637,13 @@ public boolean setRows(int rows) {
604637
return false;
605638
}
606639

640+
if (mRows == rows) {
641+
return true;
642+
}
643+
607644
mRows = rows;
608645
initVariables();
646+
generateGrid(false);
609647
invalidate();
610648
return true;
611649
}
@@ -628,8 +666,13 @@ public boolean setColumns(int columns) {
628666
return false;
629667
}
630668

669+
if (mColumns == columns) {
670+
return true;
671+
}
672+
631673
mColumns = columns;
632674
initVariables();
675+
generateGrid(false);
633676
invalidate();
634677
return true;
635678
}
@@ -653,17 +696,17 @@ public boolean setOrientation(String orientation) {
653696
return false;
654697
}
655698

656-
if (orientation.equals(mOrientation)) {
699+
if (mOrientation != null && mOrientation.equals(orientation)) {
657700
return true;
658701
}
659702

660703
mOrientation = orientation;
704+
generateGrid(true);
661705
invalidate();
662706
return true;
663707

664708
}
665709

666-
667710
/**
668711
* get the string value of spans
669712
* @return the string value of spans
@@ -681,7 +724,13 @@ public Boolean setSpans(String spans) {
681724
if (!isSpansValid(spans)) {
682725
return false;
683726
}
727+
728+
if (mStrSpans != null && mStrSpans.equals(spans)) {
729+
return true;
730+
}
731+
684732
mStrSpans = spans;
733+
generateGrid(true);
685734
invalidate();
686735
return true;
687736
}
@@ -703,7 +752,13 @@ public Boolean setSkips(String skips) {
703752
if (!isSpansValid(skips)) {
704753
return false;
705754
}
755+
756+
if (mStrSkips != null && mStrSkips.equals(skips)) {
757+
return true;
758+
}
759+
706760
mStrSkips = skips;
761+
generateGrid(true);
707762
invalidate();
708763
return true;
709764
}
@@ -719,14 +774,19 @@ public String getRowWeights() {
719774
/**
720775
* set new rowWeights value and also invoke invalidate
721776
* @param rowWeights new rowWeights value
722-
* @return rue if it succeeds otherwise false
777+
* @return true if it succeeds otherwise false
723778
*/
724779
public Boolean setRowWeights(String rowWeights) {
725780
if (!isWeightsValid(rowWeights)) {
726781
return false;
727782
}
728783

784+
if (mStrRowWeights != null && mStrRowWeights.equals(rowWeights)) {
785+
return true;
786+
}
787+
729788
mStrRowWeights = rowWeights;
789+
generateGrid(true);
730790
invalidate();
731791
return true;
732792
}
@@ -742,14 +802,75 @@ public String getColumnWeights() {
742802
/**
743803
* set new columnWeights value and also invoke invalidate
744804
* @param columnWeights new columnWeights value
745-
* @return rue if it succeeds otherwise false
805+
* @return true if it succeeds otherwise false
746806
*/
747807
public Boolean setColumnWeights(String columnWeights) {
748808
if (!isWeightsValid(columnWeights)) {
749809
return false;
750810
}
751811

812+
if (mStrColumnWeights != null && mStrColumnWeights.equals(columnWeights)) {
813+
return true;
814+
}
815+
752816
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);
753874
invalidate();
754875
return true;
755876
}

0 commit comments

Comments
 (0)