Skip to content

Commit ed528c6

Browse files
pekingmehunterstich
authored andcommitted
[ButtonGroup] Added flags to prevent unnecessary updates to child shape.
PiperOrigin-RevId: 731763436
1 parent 67b6dce commit ed528c6

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/java/com/google/android/material/button/MaterialButtonGroup.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public class MaterialButtonGroup extends LinearLayout {
131131
@Px private int spacing;
132132
@Nullable private StateListSizeChange buttonSizeChange;
133133

134+
private boolean isChildrenShapeInvalidated = true;
135+
134136
public MaterialButtonGroup(@NonNull Context context) {
135137
this(context, null);
136138
}
@@ -204,6 +206,7 @@ public void addView(@NonNull View child, int index, @Nullable ViewGroup.LayoutPa
204206
return;
205207
}
206208

209+
isChildrenShapeInvalidated = true;
207210
super.addView(child, index, params);
208211
MaterialButton buttonChild = (MaterialButton) child;
209212
setGeneratedIdIfNeeded(buttonChild);
@@ -231,6 +234,7 @@ public void onViewRemoved(View child) {
231234
originalChildStateListShapeAppearanceModels.remove(indexOfChild);
232235
}
233236

237+
isChildrenShapeInvalidated = true;
234238
updateChildShapes();
235239
adjustChildMarginsAndUpdateLayout();
236240
}
@@ -256,9 +260,11 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
256260
@VisibleForTesting
257261
void updateChildShapes() {
258262
// No need to update shape if no inside corners or outer corners are specified.
259-
if (innerCornerSize == null && groupStateListShapeAppearance == null) {
263+
if ((innerCornerSize == null && groupStateListShapeAppearance == null)
264+
|| !isChildrenShapeInvalidated) {
260265
return;
261266
}
267+
isChildrenShapeInvalidated = false;
262268
int childCount = getChildCount();
263269
int firstVisibleChildIndex = getFirstVisibleChildIndex();
264270
int lastVisibleChildIndex = getLastVisibleChildIndex();
@@ -580,6 +586,7 @@ public CornerSize getInnerCornerSize() {
580586
*/
581587
public void setInnerCornerSize(@NonNull CornerSize cornerSize) {
582588
innerCornerSize = StateListCornerSize.create(cornerSize);
589+
isChildrenShapeInvalidated = true;
583590
updateChildShapes();
584591
invalidate();
585592
}
@@ -608,6 +615,7 @@ public StateListCornerSize getInnerCornerSizeStateList() {
608615
@RestrictTo(Scope.LIBRARY_GROUP)
609616
public void setInnerCornerSizeStateList(@NonNull StateListCornerSize cornerSizeStateList) {
610617
innerCornerSize = cornerSizeStateList;
618+
isChildrenShapeInvalidated = true;
611619
updateChildShapes();
612620
invalidate();
613621
}
@@ -628,6 +636,7 @@ public ShapeAppearanceModel getShapeAppearance() {
628636
public void setShapeAppearance(@Nullable ShapeAppearanceModel shapeAppearance) {
629637
groupStateListShapeAppearance =
630638
new StateListShapeAppearanceModel.Builder(shapeAppearance).build();
639+
isChildrenShapeInvalidated = true;
631640
updateChildShapes();
632641
invalidate();
633642
}
@@ -653,6 +662,7 @@ public StateListShapeAppearanceModel getStateListShapeAppearance() {
653662
public void setStateListShapeAppearance(
654663
@Nullable StateListShapeAppearanceModel stateListShapeAppearance) {
655664
groupStateListShapeAppearance = stateListShapeAppearance;
665+
isChildrenShapeInvalidated = true;
656666
updateChildShapes();
657667
invalidate();
658668
}

0 commit comments

Comments
 (0)