Skip to content

Commit 144b515

Browse files
manabu-nakamuraleticiarossi
authored andcommitted
[Slider] Fix slider label not moving while scrolling
Resolves #3848 Resolves #3847 GIT_ORIGIN_REV_ID=630698384082464cfb40d75156ec09abfc829bed PiperOrigin-RevId: 584067588
1 parent 54d2c8b commit 144b515

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import android.view.ViewConfiguration;
6666
import android.view.ViewGroup;
6767
import android.view.ViewParent;
68+
import android.view.ViewTreeObserver;
6869
import android.view.accessibility.AccessibilityEvent;
6970
import android.view.accessibility.AccessibilityManager;
7071
import android.widget.SeekBar;
@@ -338,6 +339,26 @@ abstract class BaseSlider<
338339
private float touchPosition;
339340
@SeparationUnit private int separationUnit = UNIT_PX;
340341

342+
@NonNull private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = () -> {
343+
if (shouldAlwaysShowLabel() && isEnabled()) {
344+
Rect contentViewBounds = new Rect();
345+
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
346+
boolean isSliderVisibleOnScreen = getLocalVisibleRect(contentViewBounds);
347+
for (int i = 0; i < labels.size(); i++) {
348+
TooltipDrawable label = labels.get(i);
349+
// Get associated value for label
350+
if (i < values.size()) {
351+
positionLabel(label, values.get(i));
352+
}
353+
if (isSliderVisibleOnScreen) {
354+
ViewUtils.getContentViewOverlay(this).add(label);
355+
} else {
356+
ViewUtils.getContentViewOverlay(this).remove(label);
357+
}
358+
}
359+
}
360+
};
361+
341362
/**
342363
* Determines the behavior of the label which can be any of the following.
343364
*
@@ -1865,6 +1886,7 @@ public void setEnabled(boolean enabled) {
18651886
@Override
18661887
protected void onAttachedToWindow() {
18671888
super.onAttachedToWindow();
1889+
getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener);
18681890
// The label is attached on the Overlay relative to the content.
18691891
for (TooltipDrawable label : labels) {
18701892
attachLabelToContentView(label);
@@ -1885,7 +1907,7 @@ protected void onDetachedFromWindow() {
18851907
for (TooltipDrawable label : labels) {
18861908
detachLabelFromContentView(label);
18871909
}
1888-
1910+
getViewTreeObserver().removeOnScrollChangedListener(onScrollChangedListener);
18891911
super.onDetachedFromWindow();
18901912
}
18911913

@@ -2646,7 +2668,11 @@ private String formatValue(float value) {
26462668

26472669
private void setValueForLabel(TooltipDrawable label, float value) {
26482670
label.setText(formatValue(value));
2671+
positionLabel(label, value);
2672+
ViewUtils.getContentViewOverlay(this).add(label);
2673+
}
26492674

2675+
private void positionLabel(TooltipDrawable label, float value) {
26502676
int left =
26512677
trackSidePadding
26522678
+ (int) (normalizeValue(value) * trackWidth)
@@ -2659,8 +2685,6 @@ private void setValueForLabel(TooltipDrawable label, float value) {
26592685
Rect rect = new Rect(label.getBounds());
26602686
DescendantOffsetUtils.offsetDescendantRect(ViewUtils.getContentView(this), this, rect);
26612687
label.setBounds(rect);
2662-
2663-
ViewUtils.getContentViewOverlay(this).add(label);
26642688
}
26652689

26662690
private void invalidateTrack() {

0 commit comments

Comments
 (0)