Skip to content

Commit bc9595b

Browse files
committed
Revert "[ChipGroup] Fix ChipGroup.getCheckedChipIds() returns wrong state"
This reverts commit ba9803a.
1 parent ba9803a commit bc9595b

File tree

2 files changed

+8
-63
lines changed

2 files changed

+8
-63
lines changed

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

+7-19
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import android.view.ViewParent;
5555
import android.view.accessibility.AccessibilityEvent;
5656
import android.view.accessibility.AccessibilityNodeInfo;
57-
import android.widget.CompoundButton;
5857
import androidx.annotation.AnimatorRes;
5958
import androidx.annotation.BoolRes;
6059
import androidx.annotation.CallSuper;
@@ -152,7 +151,6 @@ public class Chip extends AppCompatCheckBox
152151
@Nullable private RippleDrawable ripple;
153152

154153
@Nullable private OnClickListener onCloseIconClickListener;
155-
@Nullable private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
156154
@Nullable private MaterialCheckable.OnCheckedChangeListener<Chip> onCheckedChangeListenerInternal;
157155
private boolean deferredCheckedValue;
158156
private boolean closeIconPressed;
@@ -253,16 +251,6 @@ public Chip(Context context, AttributeSet attrs, int defStyleAttr) {
253251
setMinHeight(minTouchTargetSize);
254252
}
255253
lastLayoutDirection = ViewCompat.getLayoutDirection(this);
256-
257-
super.setOnCheckedChangeListener(
258-
(buttonView, isChecked) -> {
259-
if (onCheckedChangeListenerInternal != null) {
260-
onCheckedChangeListenerInternal.onCheckedChanged(Chip.this, isChecked);
261-
}
262-
if (onCheckedChangeListener != null) {
263-
onCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
264-
}
265-
});
266254
}
267255

268256
@Override
@@ -719,15 +707,15 @@ public void setChecked(boolean checked) {
719707
// Defer the setChecked() call until after initialization.
720708
deferredCheckedValue = checked;
721709
} else if (chipDrawable.isCheckable()) {
710+
boolean wasChecked = isChecked();
722711
super.setChecked(checked);
723-
}
724-
}
725712

726-
@Override
727-
public void setOnCheckedChangeListener(
728-
@Nullable CompoundButton.OnCheckedChangeListener listener) {
729-
// Do not call super here - the wrapped listener set in the constructor will call the listener.
730-
onCheckedChangeListener = listener;
713+
if (wasChecked != checked) {
714+
if (onCheckedChangeListenerInternal != null) {
715+
onCheckedChangeListenerInternal.onCheckedChanged(this, checked);
716+
}
717+
}
718+
}
731719
}
732720

733721
/** Register a callback to be invoked when the close icon is clicked. */

lib/javatests/com/google/android/material/chip/ChipGroupTest.java

+1-44
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
*/
1616
package com.google.android.material.chip;
1717

18-
import android.widget.CompoundButton.OnCheckedChangeListener;
19-
import com.google.android.material.test.R;
18+
import com.google.android.material.R;
2019

21-
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
2220
import static com.google.common.truth.Truth.assertThat;
2321
import static org.junit.Assert.assertEquals;
2422
import static org.junit.Assert.assertTrue;
2523

2624
import android.content.Context;
2725
import androidx.appcompat.app.AppCompatActivity;
2826
import android.view.View;
29-
import android.widget.CompoundButton;
3027
import androidx.core.view.ViewCompat;
3128
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
3229
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat;
@@ -199,41 +196,6 @@ public void onCheckedChanged(ChipGroup group, List<Integer> checkedIds) {
199196
assertThat(checkedIds).contains(second.getId());
200197
}
201198

202-
@Test
203-
public void multipleSelection_chipListener() {
204-
chipgroup.setSingleSelection(false);
205-
206-
Chip first = (Chip) chipgroup.getChildAt(0);
207-
first.setOnCheckedChangeListener(
208-
new OnCheckedChangeListener() {
209-
@Override
210-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
211-
onChipCheckedStateChanged(buttonView, isChecked);
212-
}
213-
});
214-
215-
Chip second = (Chip) chipgroup.getChildAt(1);
216-
second.setOnCheckedChangeListener(
217-
new OnCheckedChangeListener() {
218-
@Override
219-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
220-
onChipCheckedStateChanged(buttonView, isChecked);
221-
}
222-
});
223-
224-
first.performClick();
225-
getInstrumentation().waitForIdleSync();
226-
227-
assertThat(checkedChangeCallCount).isEqualTo(1);
228-
assertThat(checkedIds).containsExactly(first.getId());
229-
230-
second.performClick();
231-
getInstrumentation().waitForIdleSync();
232-
233-
assertThat(checkedChangeCallCount).isEqualTo(2);
234-
assertThat(checkedIds).containsExactly(first.getId(), second.getId());
235-
}
236-
237199
@Test
238200
public void multiSelection_withSelectionRequired_unSelectsIfTwo() {
239201
chipgroup.setSingleSelection(false);
@@ -298,9 +260,4 @@ public void isNotSingleLine_initializesAccessibilityNodeInfo() {
298260
assertEquals(1, itemInfo.getRowIndex());
299261
assertTrue(itemInfo.isSelected());
300262
}
301-
302-
private void onChipCheckedStateChanged(CompoundButton chip, boolean checked) {
303-
checkedChangeCallCount++;
304-
checkedIds = chipgroup.getCheckedChipIds();
305-
}
306263
}

0 commit comments

Comments
 (0)