Skip to content

Commit 602a9b6

Browse files
wcshicketcham
authored andcommitted
Switch ChipGroup to inherit from FlowLayout.
PiperOrigin-RevId: 204005581
1 parent 2cb77c9 commit 602a9b6

File tree

9 files changed

+257
-3200
lines changed

9 files changed

+257
-3200
lines changed

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

Lines changed: 24 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,15 @@
2020

2121
import android.content.Context;
2222
import android.content.res.TypedArray;
23-
import android.graphics.Canvas;
24-
import android.graphics.ColorFilter;
25-
import android.graphics.PixelFormat;
2623
import android.graphics.drawable.Drawable;
2724
import android.os.Build.VERSION;
2825
import android.os.Build.VERSION_CODES;
29-
import android.os.Parcel;
3026
import android.support.annotation.BoolRes;
3127
import android.support.annotation.DimenRes;
3228
import android.support.annotation.Dimension;
3329
import android.support.annotation.IdRes;
34-
import android.support.annotation.NonNull;
3530
import 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;
3832
import com.google.android.material.internal.ThemeEnforcement;
3933
import android.util.AttributeSet;
4034
import android.view.View;
@@ -52,7 +46,7 @@
5246
* group unchecks any previously checked chip within the same group. The behavior mirrors that of
5347
* {@link android.widget.RadioGroup}.
5448
*/
55-
public class ChipGroup extends FlexboxLayout {
49+
public class ChipGroup extends FlowLayout {
5650

5751
/**
5852
* Interface definition for a callback to be invoked when the checked chip changed in this group.
@@ -68,39 +62,8 @@ public interface OnCheckedChangeListener {
6862
public void onCheckedChanged(ChipGroup group, @IdRes int checkedId);
6963
}
7064

71-
/**
72-
* {@link FlexboxLayout.LayoutParams for {@link ChipGroup}.
73-
*/
74-
public static class LayoutParams extends FlexboxLayout.LayoutParams {
75-
public LayoutParams(Context context, AttributeSet attrs) {
76-
super(context, attrs);
77-
}
78-
79-
public LayoutParams(FlexboxLayout.LayoutParams source) {
80-
super(source);
81-
}
82-
83-
public LayoutParams(ViewGroup.LayoutParams source) {
84-
super(source);
85-
}
86-
87-
public LayoutParams(int width, int height) {
88-
super(width, height);
89-
}
90-
91-
public LayoutParams(MarginLayoutParams source) {
92-
super(source);
93-
}
94-
95-
protected LayoutParams(Parcel in) {
96-
super(in);
97-
}
98-
}
99-
100-
private final SpacingDrawable spacingDrawable = new SpacingDrawable();
10165
@Dimension private int chipSpacingHorizontal;
10266
@Dimension private int chipSpacingVertical;
103-
private boolean singleLine;
10467
private boolean singleSelection;
10568

10669
@Nullable private OnCheckedChangeListener onCheckedChangeListener;
@@ -144,35 +107,9 @@ public ChipGroup(Context context, AttributeSet attrs, int defStyleAttr) {
144107
}
145108

146109
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-
153110
super.setOnHierarchyChangeListener(passThroughListener);
154111
}
155112

156-
@Override
157-
public FlexboxLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
158-
return new ChipGroup.LayoutParams(getContext(), attrs);
159-
}
160-
161-
@Override
162-
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
163-
return new ChipGroup.LayoutParams(lp);
164-
}
165-
166-
@Override
167-
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
168-
return new ChipGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
169-
}
170-
171-
@Override
172-
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
173-
return super.checkLayoutParams(p) && (p instanceof ChipGroup.LayoutParams);
174-
}
175-
176113
@Override
177114
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
178115
// the user listener is delegated to our pass-through listener
@@ -205,43 +142,38 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
205142
super.addView(child, index, params);
206143
}
207144

208-
@Override
145+
/** Deprecated. Use {@link ChipGroup#setChipSpacingHorizontal(int)} instead. */
146+
@Deprecated
209147
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);
148+
throw new UnsupportedOperationException(
149+
"Changing divider drawables have no effect. ChipGroup do not use divider drawables as "
150+
+ "spacing.");
215151
}
216152

217-
@Override
153+
/** Deprecated. Use {@link ChipGroup#setChipSpacingVertical(int)} instead. */
154+
@Deprecated
218155
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);
156+
throw new UnsupportedOperationException(
157+
"Changing divider drawables have no effect. ChipGroup do not use divider drawables as "
158+
+ "spacing.");
224159
}
225160

226-
@Override
161+
/** Deprecated. Use {@link ChipGroup#setChipSpacingHorizontal(int)} instead. */
162+
@Deprecated
227163
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);
164+
throw new UnsupportedOperationException(
165+
"Changing divider modes has no effect. ChipGroup do not use divider drawables as spacing.");
233166
}
234167

235-
@Override
168+
/** Deprecated. Use {@link ChipGroup#setChipSpacingVertical(int)} instead. */
169+
@Deprecated
236170
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);
171+
throw new UnsupportedOperationException(
172+
"Changing divider modes has no effect. ChipGroup do not use divider drawables as spacing.");
242173
}
243174

244-
@Override
175+
/** Deprecated Use {@link ChipGroup#setSingleLine(int)} instead. */
176+
@Deprecated
245177
public void setFlexWrap(int flexWrap) {
246178
throw new UnsupportedOperationException(
247179
"Changing flex wrap not allowed. ChipGroup exposes a singleLine attribute instead.");
@@ -355,6 +287,7 @@ public int getChipSpacingHorizontal() {
355287
public void setChipSpacingHorizontal(@Dimension int chipSpacingHorizontal) {
356288
if (this.chipSpacingHorizontal != chipSpacingHorizontal) {
357289
this.chipSpacingHorizontal = chipSpacingHorizontal;
290+
setItemSpacing(chipSpacingHorizontal);
358291
requestLayout();
359292
}
360293
}
@@ -374,6 +307,7 @@ public int getChipSpacingVertical() {
374307
public void setChipSpacingVertical(@Dimension int chipSpacingVertical) {
375308
if (this.chipSpacingVertical != chipSpacingVertical) {
376309
this.chipSpacingVertical = chipSpacingVertical;
310+
setLineSpacing(chipSpacingVertical);
377311
requestLayout();
378312
}
379313
}
@@ -383,17 +317,6 @@ public void setChipSpacingVerticalResource(@DimenRes int id) {
383317
setChipSpacingVertical(getResources().getDimensionPixelOffset(id));
384318
}
385319

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-
397320
/** Sets whether this chip group is single line, or reflowed multiline. */
398321
public void setSingleLine(@BoolRes int id) {
399322
setSingleLine(getResources().getBoolean(id));
@@ -426,43 +349,6 @@ public void setSingleSelection(@BoolRes int id) {
426349
setSingleSelection(getResources().getBoolean(id));
427350
}
428351

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-
466352
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
467353
@Override
468354
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

0 commit comments

Comments
 (0)