2020
2121import android .content .Context ;
2222import android .content .res .TypedArray ;
23- import android .graphics .Canvas ;
24- import android .graphics .ColorFilter ;
25- import android .graphics .PixelFormat ;
2623import android .graphics .drawable .Drawable ;
2724import android .os .Build .VERSION ;
2825import android .os .Build .VERSION_CODES ;
29- import android .os .Parcel ;
3026import android .support .annotation .BoolRes ;
3127import android .support .annotation .DimenRes ;
3228import android .support .annotation .Dimension ;
3329import android .support .annotation .IdRes ;
34- import android .support .annotation .NonNull ;
3530import android .support .annotation .Nullable ;
36- import com .google .android .material .internal .FlexWrap ;
37- import com .google .android .material .internal .FlexboxLayout ;
31+ import com .google .android .material .internal .FlowLayout ;
3832import com .google .android .material .internal .ThemeEnforcement ;
3933import android .util .AttributeSet ;
4034import android .view .View ;
4135import android .view .ViewGroup ;
36+ import android .view .ViewGroup .MarginLayoutParams ;
4237import android .widget .CompoundButton ;
4338
4439/**
5247 * group unchecks any previously checked chip within the same group. The behavior mirrors that of
5348 * {@link android.widget.RadioGroup}.
5449 */
55- public class ChipGroup extends FlexboxLayout {
50+ public class ChipGroup extends FlowLayout {
5651
5752 /**
5853 * Interface definition for a callback to be invoked when the checked chip changed in this group.
@@ -68,18 +63,15 @@ public interface OnCheckedChangeListener {
6863 public void onCheckedChanged (ChipGroup group , @ IdRes int checkedId );
6964 }
7065
66+
7167 /**
72- * {@link FlexboxLayout .LayoutParams for {@link ChipGroup}.
68+ * {@link ChipGroup .LayoutParams for {@link ChipGroup}.
7369 */
74- public static class LayoutParams extends FlexboxLayout . LayoutParams {
70+ public static class LayoutParams extends MarginLayoutParams {
7571 public LayoutParams (Context context , AttributeSet attrs ) {
7672 super (context , attrs );
7773 }
7874
79- public LayoutParams (FlexboxLayout .LayoutParams source ) {
80- super (source );
81- }
82-
8375 public LayoutParams (ViewGroup .LayoutParams source ) {
8476 super (source );
8577 }
@@ -91,16 +83,10 @@ public LayoutParams(int width, int height) {
9183 public LayoutParams (MarginLayoutParams source ) {
9284 super (source );
9385 }
94-
95- protected LayoutParams (Parcel in ) {
96- super (in );
97- }
9886 }
9987
100- private final SpacingDrawable spacingDrawable = new SpacingDrawable ();
10188 @ Dimension private int chipSpacingHorizontal ;
10289 @ Dimension private int chipSpacingVertical ;
103- private boolean singleLine ;
10490 private boolean singleSelection ;
10591
10692 @ Nullable private OnCheckedChangeListener onCheckedChangeListener ;
@@ -144,17 +130,11 @@ public ChipGroup(Context context, AttributeSet attrs, int defStyleAttr) {
144130 }
145131
146132 a .recycle ();
147-
148- setDividerDrawable (spacingDrawable );
149- setShowDivider (SHOW_DIVIDER_MIDDLE );
150- // Superclass uses presence of dividers to determine whether it needs to draw.
151- setWillNotDraw (true );
152-
153133 super .setOnHierarchyChangeListener (passThroughListener );
154134 }
155135
156136 @ Override
157- public FlexboxLayout .LayoutParams generateLayoutParams (AttributeSet attrs ) {
137+ public ViewGroup .LayoutParams generateLayoutParams (AttributeSet attrs ) {
158138 return new ChipGroup .LayoutParams (getContext (), attrs );
159139 }
160140
@@ -205,43 +185,38 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
205185 super .addView (child , index , params );
206186 }
207187
208- @ Override
188+ /** Deprecated. Use {@link ChipGroup#setChipSpacingHorizontal(int)} instead. */
189+ @ Deprecated
209190 public void setDividerDrawableHorizontal (Drawable divider ) {
210- if (divider != spacingDrawable ) {
211- throw new UnsupportedOperationException (
212- "Changing divider drawables not allowed. ChipGroup uses divider drawables as spacing." );
213- }
214- super .setDividerDrawableHorizontal (divider );
191+ throw new UnsupportedOperationException (
192+ "Changing divider drawables have no effect. ChipGroup do not use divider drawables as "
193+ + "spacing." );
215194 }
216195
217- @ Override
196+ /** Deprecated. Use {@link ChipGroup#setChipSpacingVertical(int)} instead. */
197+ @ Deprecated
218198 public void setDividerDrawableVertical (@ Nullable Drawable divider ) {
219- if (divider != spacingDrawable ) {
220- throw new UnsupportedOperationException (
221- "Changing divider drawables not allowed. ChipGroup uses divider drawables as spacing." );
222- }
223- super .setDividerDrawableVertical (divider );
199+ throw new UnsupportedOperationException (
200+ "Changing divider drawables have no effect. ChipGroup do not use divider drawables as "
201+ + "spacing." );
224202 }
225203
226- @ Override
204+ /** Deprecated. Use {@link ChipGroup#setChipSpacingHorizontal(int)} instead. */
205+ @ Deprecated
227206 public void setShowDividerHorizontal (int dividerMode ) {
228- if (dividerMode != SHOW_DIVIDER_MIDDLE ) {
229- throw new UnsupportedOperationException (
230- "Changing divider modes not allowed. ChipGroup uses divider drawables as spacing." );
231- }
232- super .setShowDividerHorizontal (dividerMode );
207+ throw new UnsupportedOperationException (
208+ "Changing divider modes has no effect. ChipGroup do not use divider drawables as spacing." );
233209 }
234210
235- @ Override
211+ /** Deprecated. Use {@link ChipGroup#setChipSpacingVertical(int)} instead. */
212+ @ Deprecated
236213 public void setShowDividerVertical (int dividerMode ) {
237- if (dividerMode != SHOW_DIVIDER_MIDDLE ) {
238- throw new UnsupportedOperationException (
239- "Changing divider modes not allowed. ChipGroup uses divider drawables as spacing." );
240- }
241- super .setShowDividerVertical (dividerMode );
214+ throw new UnsupportedOperationException (
215+ "Changing divider modes has no effect. ChipGroup do not use divider drawables as spacing." );
242216 }
243217
244- @ Override
218+ /** Deprecated Use {@link ChipGroup#setSingleLine(int)} instead. */
219+ @ Deprecated
245220 public void setFlexWrap (int flexWrap ) {
246221 throw new UnsupportedOperationException (
247222 "Changing flex wrap not allowed. ChipGroup exposes a singleLine attribute instead." );
@@ -355,6 +330,7 @@ public int getChipSpacingHorizontal() {
355330 public void setChipSpacingHorizontal (@ Dimension int chipSpacingHorizontal ) {
356331 if (this .chipSpacingHorizontal != chipSpacingHorizontal ) {
357332 this .chipSpacingHorizontal = chipSpacingHorizontal ;
333+ setItemSpacing (chipSpacingHorizontal );
358334 requestLayout ();
359335 }
360336 }
@@ -374,6 +350,7 @@ public int getChipSpacingVertical() {
374350 public void setChipSpacingVertical (@ Dimension int chipSpacingVertical ) {
375351 if (this .chipSpacingVertical != chipSpacingVertical ) {
376352 this .chipSpacingVertical = chipSpacingVertical ;
353+ setLineSpacing (chipSpacingVertical );
377354 requestLayout ();
378355 }
379356 }
@@ -383,17 +360,6 @@ public void setChipSpacingVerticalResource(@DimenRes int id) {
383360 setChipSpacingVertical (getResources ().getDimensionPixelOffset (id ));
384361 }
385362
386- /** Returns whether this chip group is single line, or reflowed multiline. */
387- public boolean isSingleLine () {
388- return singleLine ;
389- }
390-
391- /** Sets whether this chip group is single line, or reflowed multiline. */
392- public void setSingleLine (boolean singleLine ) {
393- this .singleLine = singleLine ;
394- super .setFlexWrap (singleLine ? FlexWrap .NOWRAP : FlexWrap .WRAP );
395- }
396-
397363 /** Sets whether this chip group is single line, or reflowed multiline. */
398364 public void setSingleLine (@ BoolRes int id ) {
399365 setSingleLine (getResources ().getBoolean (id ));
@@ -426,43 +392,6 @@ public void setSingleSelection(@BoolRes int id) {
426392 setSingleSelection (getResources ().getBoolean (id ));
427393 }
428394
429- /**
430- * Drawable that only has intrinsic width/height and nothing else. Intended to be used as spacing
431- * for {@link ChipGroup#setDividerDrawable(Drawable)}.
432- */
433- private class SpacingDrawable extends Drawable {
434-
435- @ Override
436- public int getIntrinsicWidth () {
437- return chipSpacingHorizontal ;
438- }
439-
440- @ Override
441- public int getIntrinsicHeight () {
442- return chipSpacingVertical ;
443- }
444-
445- @ Override
446- public void draw (@ NonNull Canvas canvas ) {
447- // No-op.
448- }
449-
450- @ Override
451- public void setAlpha (int alpha ) {
452- // No-op.
453- }
454-
455- @ Override
456- public void setColorFilter (@ Nullable ColorFilter colorFilter ) {
457- // No-op.
458- }
459-
460- @ Override
461- public int getOpacity () {
462- return PixelFormat .TRANSPARENT ;
463- }
464- }
465-
466395 private class CheckedStateTracker implements CompoundButton .OnCheckedChangeListener {
467396 @ Override
468397 public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
0 commit comments