Skip to content

Commit

Permalink
[Slider] Do not capture inflation parameters for reuse
Browse files Browse the repository at this point in the history
Resolves #2931

GIT_ORIGIN_REV_ID=6fd66149c9f67df309f29770e39d7a95eac2deea
PiperOrigin-RevId: 471813225
  • Loading branch information
pubiqq authored and imhappi committed Sep 2, 2022
1 parent 158a413 commit a137c28
Showing 1 changed file with 9 additions and 32 deletions.
41 changes: 9 additions & 32 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Expand Up @@ -255,11 +255,7 @@ abstract class BaseSlider<
private final AccessibilityManager accessibilityManager;
private AccessibilityEventSender accessibilityEventSender;

private interface TooltipDrawableFactory {
TooltipDrawable createTooltipDrawable();
}

@NonNull private final TooltipDrawableFactory labelMaker;
private int labelStyle;
@NonNull private final List<TooltipDrawable> labels = new ArrayList<>();
@NonNull private final List<L> changeListeners = new ArrayList<>();
@NonNull private final List<T> touchListeners = new ArrayList<>();
Expand Down Expand Up @@ -380,22 +376,6 @@ public BaseSlider(
activeTicksPaint.setStrokeCap(Cap.ROUND);

loadResources(context.getResources());

// Because there's currently no way to copy the TooltipDrawable we use this to make more if more
// thumbs are added.
labelMaker =
new TooltipDrawableFactory() {
@Override
public TooltipDrawable createTooltipDrawable() {
final TypedArray a =
ThemeEnforcement.obtainStyledAttributes(
getContext(), attrs, R.styleable.Slider, defStyleAttr, DEF_STYLE_RES);
TooltipDrawable d = parseLabelDrawable(getContext(), a);
a.recycle();
return d;
}
};

processAttributes(context, attrs, defStyleAttr);

setFocusable(true);
Expand Down Expand Up @@ -430,6 +410,10 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
TypedArray a =
ThemeEnforcement.obtainStyledAttributes(
context, attrs, R.styleable.Slider, defStyleAttr, DEF_STYLE_RES);

labelStyle = a.getResourceId(
R.styleable.Slider_labelStyle, R.style.Widget_MaterialComponents_Tooltip);

valueFrom = a.getFloat(R.styleable.Slider_android_valueFrom, 0.0f);
valueTo = a.getFloat(R.styleable.Slider_android_valueTo, 1.0f);
setValues(valueFrom);
Expand Down Expand Up @@ -518,16 +502,6 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
a.recycle();
}

@NonNull
private static TooltipDrawable parseLabelDrawable(
@NonNull Context context, @NonNull TypedArray a) {
return TooltipDrawable.createFromAttributes(
context,
null,
0,
a.getResourceId(R.styleable.Slider_labelStyle, R.style.Widget_MaterialComponents_Tooltip));
}

private boolean maybeIncreaseTrackSidePadding() {
int increasedSidePaddingByThumb = max(thumbRadius - defaultThumbRadius, 0);
int increasedSidePaddingByTrack = max((trackHeight - defaultTrackHeight) / 2, 0);
Expand Down Expand Up @@ -782,7 +756,10 @@ private void createLabelPool() {

// If there's not enough labels, add more.
while (labels.size() < values.size()) {
TooltipDrawable tooltipDrawable = labelMaker.createTooltipDrawable();
// Because there's currently no way to copy the TooltipDrawable we use this to make more
// if more thumbs are added.
TooltipDrawable tooltipDrawable =
TooltipDrawable.createFromAttributes(getContext(), null, 0, labelStyle);
labels.add(tooltipDrawable);
if (ViewCompat.isAttachedToWindow(this)) {
attachLabelToContentView(tooltipDrawable);
Expand Down

0 comments on commit a137c28

Please sign in to comment.