Skip to content

Commit 0356d7c

Browse files
drchenpekingme
authored andcommitted
[ChipGroup] Fix ChipGroup.getCheckedChipIds() returns wrong state
In the Chip implementation, onCheckedChangeListener was called before onCheckedChangeListenerInternal. This causes an issue that in onCheckedChangeListener's callback, the checkable group's checked state is not updated yet, therefore ChipGroup.getCheckedChipIds() will return the outdated checked state. Fixes this by overriding Chip.setOnCheckedChangeListener to get full control of the execution order between onCheckedChangeListener and onCheckedChangeListenerInternal. Resolves #2691 PiperOrigin-RevId: 449100861 (cherry picked from commit 413a047)
1 parent bef8ca1 commit 0356d7c

File tree

55 files changed

+636
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+636
-58
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
mavenCentral()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:4.0.0'
8+
classpath 'com.android.tools.build:gradle:7.1.3'
99
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1010
}
1111
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip

lib/java/com/google/android/material/chip/Chip.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.android.material.chip;
1818

19+
import android.widget.CompoundButton.OnCheckedChangeListener;
1920
import com.google.android.material.R;
2021

2122
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
@@ -54,6 +55,7 @@
5455
import android.view.ViewParent;
5556
import android.view.accessibility.AccessibilityEvent;
5657
import android.view.accessibility.AccessibilityNodeInfo;
58+
import android.widget.CompoundButton;
5759
import androidx.annotation.AnimatorRes;
5860
import androidx.annotation.BoolRes;
5961
import androidx.annotation.CallSuper;
@@ -151,6 +153,7 @@ public class Chip extends AppCompatCheckBox
151153
@Nullable private RippleDrawable ripple;
152154

153155
@Nullable private OnClickListener onCloseIconClickListener;
156+
@Nullable private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
154157
@Nullable private MaterialCheckable.OnCheckedChangeListener<Chip> onCheckedChangeListenerInternal;
155158
private boolean deferredCheckedValue;
156159
private boolean closeIconPressed;
@@ -251,6 +254,19 @@ public Chip(Context context, AttributeSet attrs, int defStyleAttr) {
251254
setMinHeight(minTouchTargetSize);
252255
}
253256
lastLayoutDirection = ViewCompat.getLayoutDirection(this);
257+
258+
super.setOnCheckedChangeListener(
259+
new CompoundButton.OnCheckedChangeListener() {
260+
@Override
261+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
262+
if (onCheckedChangeListenerInternal != null) {
263+
onCheckedChangeListenerInternal.onCheckedChanged(Chip.this, isChecked);
264+
}
265+
if (onCheckedChangeListener != null) {
266+
onCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
267+
}
268+
}
269+
});
254270
}
255271

256272
@Override
@@ -707,17 +723,17 @@ public void setChecked(boolean checked) {
707723
// Defer the setChecked() call until after initialization.
708724
deferredCheckedValue = checked;
709725
} else if (chipDrawable.isCheckable()) {
710-
boolean wasChecked = isChecked();
711726
super.setChecked(checked);
712-
713-
if (wasChecked != checked) {
714-
if (onCheckedChangeListenerInternal != null) {
715-
onCheckedChangeListenerInternal.onCheckedChanged(this, checked);
716-
}
717-
}
718727
}
719728
}
720729

730+
@Override
731+
public void setOnCheckedChangeListener(
732+
@Nullable CompoundButton.OnCheckedChangeListener listener) {
733+
// Do not call super here - the wrapped listener set in the constructor will call the listener.
734+
onCheckedChangeListener = listener;
735+
}
736+
721737
/** Register a callback to be invoked when the close icon is clicked. */
722738
public void setOnCloseIconClickListener(OnClickListener listener) {
723739
this.onCloseIconClickListener = listener;

lib/javatests/com/google/android/material/appbar/MaterialToolbarTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.android.material.appbar;
1717

18-
import com.google.android.material.R;
18+
import com.google.android.material.test.R;
1919

2020
import static com.google.common.truth.Truth.assertThat;
2121

lib/javatests/com/google/android/material/badge/BadgeDrawableTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.android.material.badge;
1717

18-
import com.google.android.material.R;
18+
import com.google.android.material.test.R;
1919

2020
import static com.google.common.truth.Truth.assertThat;
2121

lib/javatests/com/google/android/material/badge/BadgeUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.android.material.badge;
1717

18-
import com.google.android.material.R;
18+
import com.google.android.material.test.R;
1919

2020
import static com.google.common.truth.Truth.assertThat;
2121

lib/javatests/com/google/android/material/bottomnavigation/BottomNavigationViewTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.android.material.bottomnavigation;
1717

18-
import com.google.android.material.R;
18+
import com.google.android.material.test.R;
1919

2020
import static com.google.common.truth.Truth.assertThat;
2121

lib/javatests/com/google/android/material/bottomsheet/BottomSheetBehaviorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.android.material.bottomsheet;
1717

18-
import com.google.android.material.R;
18+
import com.google.android.material.test.R;
1919

2020
import static com.google.common.truth.Truth.assertThat;
2121

lib/javatests/com/google/android/material/button/MaterialButtonTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.android.material.button;
1717

18-
import com.google.android.material.R;
18+
import com.google.android.material.test.R;
1919

2020
import static com.google.common.truth.Truth.assertThat;
2121

lib/javatests/com/google/android/material/button/MaterialButtonToggleGroupTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.android.material.button;
1818

19-
import com.google.android.material.R;
19+
import com.google.android.material.test.R;
2020

2121
import static android.view.View.GONE;
2222
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

0 commit comments

Comments
 (0)