Skip to content

Commit 884465b

Browse files
pubiqqhunterstich
authored andcommitted
[Slider] Use a uniform way to update labels
Resolves #4093 GIT_ORIGIN_REV_ID=7db4936713bd31a070d4bb1d9743d99222840184 PiperOrigin-RevId: 614741678 (cherry picked from commit 3bc6612)
1 parent 292ad4c commit 884465b

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

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

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -348,25 +348,7 @@ abstract class BaseSlider<
348348

349349
@NonNull
350350
private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener =
351-
() -> {
352-
if (shouldAlwaysShowLabel() && isEnabled()) {
353-
Rect contentViewBounds = new Rect();
354-
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
355-
boolean isSliderVisibleOnScreen = getLocalVisibleRect(contentViewBounds);
356-
for (int i = 0; i < labels.size(); i++) {
357-
TooltipDrawable label = labels.get(i);
358-
// Get associated value for label
359-
if (i < values.size()) {
360-
positionLabel(label, values.get(i));
361-
}
362-
if (isSliderVisibleOnScreen) {
363-
ViewUtils.getContentViewOverlay(this).add(label);
364-
} else {
365-
ViewUtils.getContentViewOverlay(this).remove(label);
366-
}
367-
}
368-
}
369-
};
351+
this::updateLabels;
370352

371353
/**
372354
* Determines the behavior of the label which can be any of the following.
@@ -2029,12 +2011,7 @@ protected void onDraw(@NonNull Canvas canvas) {
20292011
maybeDrawCompatHalo(canvas, trackWidth, yCenter);
20302012
}
20312013

2032-
// Draw labels if there is an active thumb or the labels are always visible.
2033-
if ((activeThumbIdx != -1 || shouldAlwaysShowLabel()) && isEnabled()) {
2034-
ensureLabelsAdded();
2035-
} else {
2036-
ensureLabelsRemoved();
2037-
}
2014+
updateLabels();
20382015

20392016
drawThumbs(canvas, trackWidth, yCenter);
20402017
}
@@ -2660,6 +2637,37 @@ public void onAnimationUpdate(ValueAnimator animation) {
26602637
return animator;
26612638
}
26622639

2640+
private void updateLabels() {
2641+
switch (labelBehavior) {
2642+
case LABEL_GONE:
2643+
ensureLabelsRemoved();
2644+
break;
2645+
case LABEL_VISIBLE:
2646+
if (isEnabled() && isSliderVisibleOnScreen()) {
2647+
ensureLabelsAdded();
2648+
} else {
2649+
ensureLabelsRemoved();
2650+
}
2651+
break;
2652+
case LABEL_FLOATING:
2653+
case LABEL_WITHIN_BOUNDS:
2654+
if (activeThumbIdx != -1 && isEnabled()) {
2655+
ensureLabelsAdded();
2656+
} else {
2657+
ensureLabelsRemoved();
2658+
}
2659+
break;
2660+
default:
2661+
throw new IllegalArgumentException("Unexpected labelBehavior: " + labelBehavior);
2662+
}
2663+
}
2664+
2665+
private boolean isSliderVisibleOnScreen() {
2666+
final Rect contentViewBounds = new Rect();
2667+
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
2668+
return getLocalVisibleRect(contentViewBounds);
2669+
}
2670+
26632671
private void ensureLabelsRemoved() {
26642672
// If the labels are animated in or in the process of animating in, create and start a new
26652673
// animator to animate out the labels and remove them once the animation ends.
@@ -2683,11 +2691,6 @@ public void onAnimationEnd(Animator animation) {
26832691
}
26842692

26852693
private void ensureLabelsAdded() {
2686-
if (labelBehavior == LABEL_GONE) {
2687-
// If the label shouldn't be drawn we can skip this.
2688-
return;
2689-
}
2690-
26912694
// If the labels are not animating in, start an animator to show them. ensureLabelsAdded will
26922695
// be called multiple times by BaseSlider's draw method, making this check necessary to avoid
26932696
// creating and starting an animator for each draw call.

0 commit comments

Comments
 (0)