Skip to content

Commit ca0110d

Browse files
ymarianhunterstich
authored andcommitted
[RangeSlider] Fixed number of values changing in RangeSlider
#1460 #1393 PiperOrigin-RevId: 319980426 (cherry picked from commit 4eea03d)
1 parent 0e41541 commit ca0110d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,22 @@ private void setValuesInternal(@NonNull ArrayList<Float> values) {
659659
private void createLabelPool() {
660660
// If there are too many labels, remove the extra ones from the end.
661661
if (labels.size() > values.size()) {
662-
labels.subList(values.size(), labels.size()).clear();
662+
List<TooltipDrawable> tooltipDrawables = labels.subList(values.size(), labels.size());
663+
for (TooltipDrawable label : tooltipDrawables) {
664+
if (ViewCompat.isAttachedToWindow(this)) {
665+
detachLabelFromContentView(label);
666+
}
667+
}
668+
tooltipDrawables.clear();
663669
}
664670

665671
// If there's not enough labels, add more.
666672
while (labels.size() < values.size()) {
667-
labels.add(labelMaker.createTooltipDrawable());
673+
TooltipDrawable tooltipDrawable = labelMaker.createTooltipDrawable();
674+
labels.add(tooltipDrawable);
675+
if (ViewCompat.isAttachedToWindow(this)) {
676+
attachLabelToContentView(tooltipDrawable);
677+
}
668678
}
669679

670680
// Add a stroke if there is more than one label for when they overlap.
@@ -1241,27 +1251,35 @@ protected void onAttachedToWindow() {
12411251
super.onAttachedToWindow();
12421252
// The label is attached on the Overlay relative to the content.
12431253
for (TooltipDrawable label : labels) {
1244-
label.setRelativeToView(ViewUtils.getContentView(this));
1254+
attachLabelToContentView(label);
12451255
}
12461256
}
12471257

1258+
private void attachLabelToContentView(TooltipDrawable label) {
1259+
label.setRelativeToView(ViewUtils.getContentView(this));
1260+
}
1261+
12481262
@Override
12491263
protected void onDetachedFromWindow() {
12501264
if (accessibilityEventSender != null) {
12511265
removeCallbacks(accessibilityEventSender);
12521266
}
12531267

12541268
for (TooltipDrawable label : labels) {
1255-
ViewOverlayImpl contentViewOverlay = ViewUtils.getContentViewOverlay(this);
1256-
if (contentViewOverlay != null) {
1257-
contentViewOverlay.remove(label);
1258-
label.detachView(ViewUtils.getContentView(this));
1259-
}
1269+
detachLabelFromContentView(label);
12601270
}
12611271

12621272
super.onDetachedFromWindow();
12631273
}
12641274

1275+
private void detachLabelFromContentView(TooltipDrawable label) {
1276+
ViewOverlayImpl contentViewOverlay = ViewUtils.getContentViewOverlay(this);
1277+
if (contentViewOverlay != null) {
1278+
contentViewOverlay.remove(label);
1279+
label.detachView(ViewUtils.getContentView(this));
1280+
}
1281+
}
1282+
12651283
@Override
12661284
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12671285
super.onMeasure(

0 commit comments

Comments
 (0)