6565import android .view .ViewConfiguration ;
6666import android .view .ViewGroup ;
6767import android .view .ViewParent ;
68+ import android .view .ViewTreeObserver ;
6869import android .view .accessibility .AccessibilityEvent ;
6970import android .view .accessibility .AccessibilityManager ;
7071import 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