Skip to content

Commit

Permalink
[ChipGroup] Refactor chip group selection logic to a standalone class
Browse files Browse the repository at this point in the history
This CL pulls out the checkable group logic to a standalone class, which can be shared by different UX components. It also makes chip groups support multiple selection better with introducing a new listener based on the multiple selection and deprecates the old listener which only supports single selection.

PiperOrigin-RevId: 427204476
  • Loading branch information
drchen authored and raajkumars committed Feb 9, 2022
1 parent 0de6ad4 commit f3c6430
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 157 deletions.
28 changes: 16 additions & 12 deletions lib/java/com/google/android/material/chip/Chip.java
Expand Up @@ -18,6 +18,7 @@

import com.google.android.material.R;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;

import android.annotation.SuppressLint;
Expand Down Expand Up @@ -64,6 +65,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.core.view.ViewCompat;
Expand All @@ -73,6 +75,7 @@
import androidx.customview.widget.ExploreByTouchHelper;
import com.google.android.material.animation.MotionSpec;
import com.google.android.material.chip.ChipDrawable.Delegate;
import com.google.android.material.internal.MaterialCheckable;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
Expand Down Expand Up @@ -112,8 +115,8 @@
* </ul>
*
* <p>You can register a listener on the main chip with {@link #setOnClickListener(OnClickListener)}
* or {@link #setOnCheckedChangeListener(OnCheckedChangeListener)}. You can register a listener on
* the close icon with {@link #setOnCloseIconClickListener(OnClickListener)}.
* or {@link #setOnCheckedChangeListener(AppCompatCheckBox.OnCheckedChangeListener)}. You can
* register a listener on the close icon with {@link #setOnCloseIconClickListener(OnClickListener)}.
*
* <p>For proper rendering of the ancestor TextView in RTL mode, call {@link
* #setLayoutDirection(int)} with <code>View.LAYOUT_DIRECTION_LOCALE</code>. By default, TextView's
Expand All @@ -123,7 +126,8 @@
*
* @see ChipDrawable
*/
public class Chip extends AppCompatCheckBox implements Delegate, Shapeable {
public class Chip extends AppCompatCheckBox
implements Delegate, Shapeable, MaterialCheckable<Chip> {

private static final String TAG = "Chip";

Expand All @@ -147,7 +151,7 @@ public class Chip extends AppCompatCheckBox implements Delegate, Shapeable {
@Nullable private RippleDrawable ripple;

@Nullable private OnClickListener onCloseIconClickListener;
@Nullable private OnCheckedChangeListener onCheckedChangeListenerInternal;
@Nullable private MaterialCheckable.OnCheckedChangeListener<Chip> onCheckedChangeListenerInternal;
private boolean deferredCheckedValue;
private boolean closeIconPressed;
private boolean closeIconHovered;
Expand Down Expand Up @@ -713,14 +717,6 @@ public void setChecked(boolean checked) {
}
}

/**
* Register a callback to be invoked when the checked state of this chip changes. This callback is
* used for internal purpose only.
*/
void setOnCheckedChangeListenerInternal(OnCheckedChangeListener listener) {
onCheckedChangeListenerInternal = listener;
}

/** Register a callback to be invoked when the close icon is clicked. */
public void setOnCloseIconClickListener(OnClickListener listener) {
this.onCloseIconClickListener = listener;
Expand Down Expand Up @@ -962,6 +958,14 @@ public PointerIcon onResolvePointerIcon(@NonNull MotionEvent event, int pointerI
return null;
}

/** @hide */
@RestrictTo(LIBRARY_GROUP)
@Override
public void setInternalOnCheckedChangeListener(
@Nullable MaterialCheckable.OnCheckedChangeListener<Chip> listener) {
onCheckedChangeListenerInternal = listener;
}

/** Provides a virtual view hierarchy for the close icon. */
private class ChipTouchHelper extends ExploreByTouchHelper {

Expand Down

0 comments on commit f3c6430

Please sign in to comment.