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 d6f8c3d49..44175e050 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 @@ -233,17 +233,32 @@ public void onAttachedToWindow() { mContainer = (ConstraintLayout) getParent(); mConstraintSet.clone(mContainer); - generateGrid(); + generateGrid(false); } /** * generate the Grid form based on the input attributes + * @param isUpdate whether to update the existing grid (true) or create a new one (false) * @return true if all the inputs are valid else false */ - private boolean generateGrid() { + private boolean generateGrid(boolean isUpdate) { + if (mContainer == null || mConstraintSet == null) { + return false; + } + + if (isUpdate) { + for (int i = 0; i < mPositionMatrix.length; i++) { + for (int j = 0; j < mPositionMatrix[0].length; j++) { + mPositionMatrix[i][j] = true; + } + } + mSpanIds.clear(); + } + + mNextAvailableIndex = 0; boolean isSuccess = true; - createGuidelines(mRows, mColumns); + createGuidelines(mRows, mColumns, isUpdate); if (mStrSkips != null && !mStrSkips.trim().isEmpty()) { HashMap> mSkipMap = parseSpans(mStrSkips); @@ -305,8 +320,9 @@ private float[] parseWeights(int size, String str) { * create vertical and horizontal guidelines based on mRows and mColumns * @param rows number of rows is required for grid * @param columns number of columns is required for grid + * @param isUpdate whether to update existing guidelines (true) or create new ones (false) */ - private void createGuidelines(int rows, int columns) { + private void createGuidelines(int rows, int columns, boolean isUpdate) { float[] rowWeights = parseWeights(rows, mStrRowWeights); float[] columnWeights = parseWeights(columns, mStrColumnWeights); @@ -316,11 +332,21 @@ private void createGuidelines(int rows, int columns) { columns + 1, columnWeights); for (int i = 0; i < mHorizontalGuideLines.length; i++) { + if (isUpdate) { + updateGuideLinePosition(mHorizontalGuideLines[i], horizontalPositions[i]); + continue; + } + mHorizontalGuideLines[i] = getNewGuideline(myContext, ConstraintLayout.LayoutParams.HORIZONTAL, horizontalPositions[i]); mContainer.addView(mHorizontalGuideLines[i]); } for (int i = 0; i < mVerticalGuideLines.length; i++) { + if (isUpdate) { + updateGuideLinePosition(mVerticalGuideLines[i], verticalPositions[i]); + continue; + } + mVerticalGuideLines[i] = getNewGuideline(myContext, ConstraintLayout.LayoutParams.VERTICAL, verticalPositions[i]); mContainer.addView(mVerticalGuideLines[i]); @@ -347,6 +373,13 @@ private Guideline getNewGuideline(Context context, int orientation, float positi return guideline; } + private void updateGuideLinePosition(Guideline guideline, float position) { + ConstraintLayout.LayoutParams params = + (ConstraintLayout.LayoutParams) guideline.getLayoutParams(); + params.guidePercent = position; + guideline.setLayoutParams(params); + } + /** * Connect the view to the corresponding guidelines based on the input params * @param viewId the Id of the view @@ -604,8 +637,13 @@ public boolean setRows(int rows) { return false; } + if (mRows == rows) { + return true; + } + mRows = rows; initVariables(); + generateGrid(false); invalidate(); return true; } @@ -628,8 +666,13 @@ public boolean setColumns(int columns) { return false; } + if (mColumns == columns) { + return true; + } + mColumns = columns; initVariables(); + generateGrid(false); invalidate(); return true; } @@ -653,17 +696,17 @@ public boolean setOrientation(String orientation) { return false; } - if (orientation.equals(mOrientation)) { + if (mOrientation != null && mOrientation.equals(orientation)) { return true; } mOrientation = orientation; + generateGrid(true); invalidate(); return true; } - /** * get the string value of spans * @return the string value of spans @@ -681,7 +724,13 @@ public Boolean setSpans(String spans) { if (!isSpansValid(spans)) { return false; } + + if (mStrSpans != null && mStrSpans.equals(spans)) { + return true; + } + mStrSpans = spans; + generateGrid(true); invalidate(); return true; } @@ -703,7 +752,13 @@ public Boolean setSkips(String skips) { if (!isSpansValid(skips)) { return false; } + + if (mStrSkips != null && mStrSkips.equals(skips)) { + return true; + } + mStrSkips = skips; + generateGrid(true); invalidate(); return true; } @@ -719,14 +774,19 @@ public String getRowWeights() { /** * set new rowWeights value and also invoke invalidate * @param rowWeights new rowWeights value - * @return rue if it succeeds otherwise false + * @return true if it succeeds otherwise false */ public Boolean setRowWeights(String rowWeights) { if (!isWeightsValid(rowWeights)) { return false; } + if (mStrRowWeights != null && mStrRowWeights.equals(rowWeights)) { + return true; + } + mStrRowWeights = rowWeights; + generateGrid(true); invalidate(); return true; } @@ -742,14 +802,75 @@ public String getColumnWeights() { /** * set new columnWeights value and also invoke invalidate * @param columnWeights new columnWeights value - * @return rue if it succeeds otherwise false + * @return true if it succeeds otherwise false */ public Boolean setColumnWeights(String columnWeights) { if (!isWeightsValid(columnWeights)) { return false; } + if (mStrColumnWeights != null && mStrColumnWeights.equals(columnWeights)) { + return true; + } + mStrColumnWeights = columnWeights; + generateGrid(true); + invalidate(); + return true; + } + + /** + * get the value of horizontalGaps + * @return the value of horizontalGaps + */ + public int getHorizontalGaps() { + return mHorizontalGaps; + } + + /** + * set new horizontalGaps value and also invoke invalidate + * @param horizontalGaps new horizontalGaps value + * @return true if it succeeds otherwise false + */ + public boolean setHorizontalGaps(int horizontalGaps) { + if (horizontalGaps < 0) { + return false; + } + + if (mHorizontalGaps == horizontalGaps) { + return true; + } + + mHorizontalGaps = horizontalGaps; + generateGrid(true); + invalidate(); + return true; + } + + /** + * get the value of verticalGaps + * @return the value of verticalGaps + */ + public int getVerticalGaps() { + return mVerticalGaps; + } + + /** + * set new verticalGaps value and also invoke invalidate + * @param verticalGaps new verticalGaps value + * @return true if it succeeds otherwise false + */ + public boolean setVerticalGaps(int verticalGaps) { + if (verticalGaps < 0) { + return false; + } + + if (mVerticalGaps == verticalGaps) { + return true; + } + + mVerticalGaps = verticalGaps; + generateGrid(true); invalidate(); return true; } diff --git a/projects/GridExperiments/app/src/main/java/android/support/constraint/app/MainActivity.java b/projects/GridExperiments/app/src/main/java/android/support/constraint/app/MainActivity.java index 94062c30e..618fd5d6c 100644 --- a/projects/GridExperiments/app/src/main/java/android/support/constraint/app/MainActivity.java +++ b/projects/GridExperiments/app/src/main/java/android/support/constraint/app/MainActivity.java @@ -17,15 +17,180 @@ package android.support.constraint.app; import android.os.Bundle; +import android.view.View; import androidx.appcompat.app.AppCompatActivity; +import androidx.constraintlayout.helper.widget.Grid; +/** + * MainActivity for Grid helper + */ +public class MainActivity extends AppCompatActivity { + + private Grid mGrid; + private int mHorizontalGapsIndex = 0; + private int mVerticalGapsIndex = 0; + private int mRowSize = 3; + private int mColSize = 3; + private int mRowIndex = 0; + private int mColIndex = 0; + private int mRowWeightsIndex = 0; + private int mColumnWeightsIndex = 0; + private int mSkipsIndex = 0; + private int mSpansIndex = 0; + private int mOrientationIndex = 0; + + private int[] mGaps = new int[] {0, 20}; + private int[] mSizes = new int[] {3, 4}; + private String[] mThreeWeights = new String[] {"1,1,1", "1,2,2"}; + private String[] mFourWeights = new String[] {"1,1,1,1", "1,2,1,1"}; + private String[] mSkips = new String[] {"", "0:1x2, 4:1x1"}; + private String[] mSpans = new String[] {"", "6:1x2"}; + private String[] mOrientations = new String[] {"horizontal", "vertical"}; -public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); + setContentView(R.layout.responsive); + + mGrid = findViewById(R.id.grid); + mGrid.setRows(mSizes[0]); + mGrid.setColumns(mSizes[0]); + mGrid.setRowWeights(mThreeWeights[0]); + mGrid.setColumnWeights(mThreeWeights[0]); + mGrid.setSkips(mSkips[0]); + mGrid.setSpans(mSpans[0]); + mGrid.setVerticalGaps(mGaps[0]); + mGrid.setHorizontalGaps(mGaps[0]); + mGrid.setOrientation(mOrientations[0]); + } + + /** + * toggle rows value + * @param v + */ + public void toggleRows(View v) { + mRowSize = mSizes[++mRowIndex % 2]; + mGrid.setRows(mRowSize); + } + + /** + * toggle columns value + * @param v + */ + public void toggleColumns(View v) { + mColSize = mSizes[++mColIndex % 2]; + mGrid.setColumns(mColSize); + } + + /** + * toggle rowWeights value + * @param v + */ + public void toggleRowWeights(View v) { + if (mRowSize == 3) { + mGrid.setRowWeights(mThreeWeights[++mRowWeightsIndex % 2]); + } else { + mGrid.setRowWeights(mFourWeights[++mRowWeightsIndex % 2]); + } + } + + /** + * toggle columnWeigths value + * @param v + */ + public void toggleColWeights(View v) { + if (mColSize == 3) { + mGrid.setColumnWeights(mThreeWeights[++mColumnWeightsIndex % 2]); + } else { + mGrid.setColumnWeights(mFourWeights[++mColumnWeightsIndex % 2]); + } + + } + + /** + * toggle verticalGaps value + * @param v + */ + public void toggleVerticalGaps(View v) { + mGrid.setVerticalGaps(mGaps[++mVerticalGapsIndex % 2]); + } + + /** + * toggle horizontalGaps value + * @param v + */ + public void toggleHorizontalGaps(View v) { + mGrid.setHorizontalGaps(mGaps[++mHorizontalGapsIndex % 2]); + } + + /** + * toggle skips value + * @param v + */ + public void toggleSkips(View v) { + mGrid.setSkips(mSkips[++mSkipsIndex % 2]); + } + + /** + * toggle spans value + * @param v + */ + public void toggleSpans(View v) { + mGrid.setSpans(mSpans[++mSpansIndex % 2]); + } + + /** + * toggle orientation value + * @param v + */ + public void toggleOrientation(View v) { + mGrid.setOrientation(mOrientations[++mOrientationIndex % 2]); + } + + /** + * toggle all value + * @param v + */ + public void toggleAll(View v) { + toggleRows(v); + toggleColumns(v); + toggleRowWeights(v); + toggleColWeights(v); + toggleVerticalGaps(v); + toggleHorizontalGaps(v); + toggleSkips(v); + toggleSpans(v); + toggleOrientation(v); + } + + /** + * restore to the default value. + * @param v + */ + public void restore(View v) { + mHorizontalGapsIndex = 0; + mVerticalGapsIndex = 0; + mRowSize = 3; + mColSize = 3; + mRowIndex = 0; + mColIndex = 0; + mRowWeightsIndex = 0; + mColumnWeightsIndex = 0; + mSkipsIndex = 0; + mSpansIndex = 0; + mOrientationIndex = 0; + + mGrid = findViewById(R.id.grid); + mGrid.setRows(mSizes[mRowIndex]); + mGrid.setColumns(mSizes[mColIndex]); + mGrid.setRowWeights(mThreeWeights[mRowWeightsIndex]); + mGrid.setColumnWeights(mThreeWeights[mColumnWeightsIndex]); + mGrid.setSkips(mSkips[mSkipsIndex]); + mGrid.setSpans(mSpans[mSpansIndex]); + mGrid.setVerticalGaps(mGaps[mVerticalGapsIndex]); + mGrid.setHorizontalGaps(mGaps[mHorizontalGapsIndex]); + mGrid.setOrientation(mOrientations[mOrientationIndex]); } } diff --git a/projects/GridExperiments/app/src/main/res/layout/activity_main.xml b/projects/GridExperiments/app/src/main/res/layout/activity_main.xml index 0db1b3079..6e1123ad8 100644 --- a/projects/GridExperiments/app/src/main/res/layout/activity_main.xml +++ b/projects/GridExperiments/app/src/main/res/layout/activity_main.xml @@ -58,10 +58,4 @@ tools:layout_editor_absoluteX="141dp" tools:layout_editor_absoluteY="545dp" /> -