From b617c1e6b299d6c9d8159266290f179dbababb21 Mon Sep 17 00:00:00 2001 From: "Jian-Syuan (Shane) Wong" Date: Tue, 24 May 2022 10:04:30 -0700 Subject: [PATCH] Grid helper improvement part2 --- .../constraintlayout/helper/widget/Grid.java | 149 +++++++++++++++++- 1 file changed, 143 insertions(+), 6 deletions(-) diff --git a/constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/Grid.java b/constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/Grid.java index 6495df7c2..d6f8c3d49 100644 --- a/constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/Grid.java +++ b/constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/Grid.java @@ -82,6 +82,9 @@ public class Grid extends VirtualLayout { private static final String TAG = "Grid"; private static final String VERTICAL = "vertical"; + private static final String HORIZONTAL = "horizontal"; + private final int mMaxRows = 50; // maximum number of rows can be specified. + private final int mMaxColumns = 50; // maximum number of columns can be specified. private final ConstraintSet mConstraintSet = new ConstraintSet(); ConstraintLayout mContainer; @@ -444,8 +447,8 @@ private Pair getNextPosition() { } /** - * Check if the value of the skips is valid - * @param str skips in string format + * Check if the value of the spans/skips is valid + * @param str spans/skips in string format * @return true if it is valid else false */ private boolean isSpansValid(String str) { @@ -453,6 +456,16 @@ private boolean isSpansValid(String str) { return true; } + /** + * Check if the value of the rowWeights or columnsWeights is valid + * @param str rowWeights/columnsWeights in string format + * @return true if it is valid else false + */ + private boolean isWeightsValid(String str) { + // TODO: check string has a valid format. + return true; + } + /** * parse the skips/spans in the string format into a HashMap> * the format of the input string is index:row_spanxcol_span. @@ -573,6 +586,84 @@ private float[] getLinePositions(float min, float max, int numPositions, float[] return positions; } + /** + * get the value of rows + * @return the value of rows + */ + public int getRows() { + return mRows; + } + + /** + * set new rows value and also invoke initVariables and invalidate + * @param rows new rows value + * @return true if it succeeds otherwise false + */ + public boolean setRows(int rows) { + if (rows < 2 || rows > mMaxRows) { + return false; + } + + mRows = rows; + initVariables(); + invalidate(); + return true; + } + + /** + * get the value of columns + * @return the value of columns + */ + public int getColumns() { + return mColumns; + } + + /** + * set new columns value and also invoke initVariables and invalidate + * @param columns new rows value + * @return true if it succeeds otherwise false + */ + public boolean setColumns(int columns) { + if (columns < 2 || columns > mMaxColumns) { + return false; + } + + mColumns = columns; + initVariables(); + invalidate(); + return true; + } + + + /** + * get the value of orientation + * @return the value of orientation + */ + public String getOrientation() { + return mOrientation; + } + + /** + * set new orientation value and also invoke invalidate + * @param orientation new orientation value + * @return true if it succeeds otherwise false + */ + public boolean setOrientation(String orientation) { + if (!(orientation.equals(HORIZONTAL) || orientation.equals(VERTICAL))) { + return false; + } + + if (orientation.equals(mOrientation)) { + return true; + } + + mOrientation = orientation; + invalidate(); + return true; + + } + + /** * get the string value of spans * @return the string value of spans @@ -582,7 +673,7 @@ public String getSpans() { } /** - * set new spans value and also invoke requestLayout + * set new spans value and also invoke invalidate * @param spans new spans value * @return true if it succeeds otherwise false */ @@ -591,7 +682,7 @@ public Boolean setSpans(String spans) { return false; } mStrSpans = spans; - requestLayout(); + invalidate(); return true; } @@ -604,7 +695,7 @@ public String getSkips() { } /** - * set new skips value and also invoke requestLayout + * set new skips value and also invoke invalidate * @param skips new spans value * @return true if it succeeds otherwise false */ @@ -613,7 +704,53 @@ public Boolean setSkips(String skips) { return false; } mStrSkips = skips; - requestLayout(); + invalidate(); + return true; + } + + /** + * get the string value of rowWeights + * @return the string value of rowWeights + */ + public String getRowWeights() { + return mStrRowWeights; + } + + /** + * set new rowWeights value and also invoke invalidate + * @param rowWeights new rowWeights value + * @return rue if it succeeds otherwise false + */ + public Boolean setRowWeights(String rowWeights) { + if (!isWeightsValid(rowWeights)) { + return false; + } + + mStrRowWeights = rowWeights; + invalidate(); + return true; + } + + /** + * get the string value of columnWeights + * @return the string value of columnWeights + */ + public String getColumnWeights() { + return mStrColumnWeights; + } + + /** + * set new columnWeights value and also invoke invalidate + * @param columnWeights new columnWeights value + * @return rue if it succeeds otherwise false + */ + public Boolean setColumnWeights(String columnWeights) { + if (!isWeightsValid(columnWeights)) { + return false; + } + + mStrColumnWeights = columnWeights; + invalidate(); return true; } }