57
57
import android .os .Parcel ;
58
58
import android .os .Parcelable ;
59
59
import androidx .appcompat .content .res .AppCompatResources ;
60
+ import android .text .TextUtils ;
60
61
import android .util .AttributeSet ;
61
62
import android .util .Log ;
62
63
import android .view .KeyEvent ;
65
66
import android .view .ViewConfiguration ;
66
67
import android .view .ViewGroup ;
67
68
import android .view .ViewParent ;
69
+ import android .view .ViewTreeObserver ;
68
70
import android .view .accessibility .AccessibilityEvent ;
69
71
import android .view .accessibility .AccessibilityManager ;
70
72
import android .widget .SeekBar ;
@@ -338,6 +340,22 @@ abstract class BaseSlider<
338
340
private float touchPosition ;
339
341
@ SeparationUnit private int separationUnit = UNIT_PX ;
340
342
343
+ @ NonNull private final ViewTreeObserver .OnScrollChangedListener onScrollChangedListener = () -> {
344
+ if (shouldAlwaysShowLabel () && isEnabled ()) {
345
+ Rect contentViewBounds = new Rect ();
346
+ ViewUtils .getContentView (this ).getHitRect (contentViewBounds );
347
+ boolean isSliderVisibleOnScreen = getLocalVisibleRect (contentViewBounds );
348
+ for (TooltipDrawable label : labels ) {
349
+ positionLabel (label );
350
+ if (isSliderVisibleOnScreen ) {
351
+ ViewUtils .getContentViewOverlay (this ).add (label );
352
+ } else {
353
+ ViewUtils .getContentViewOverlay (this ).remove (label );
354
+ }
355
+ }
356
+ }
357
+ };
358
+
341
359
/**
342
360
* Determines the behavior of the label which can be any of the following.
343
361
*
@@ -1865,6 +1883,7 @@ public void setEnabled(boolean enabled) {
1865
1883
@ Override
1866
1884
protected void onAttachedToWindow () {
1867
1885
super .onAttachedToWindow ();
1886
+ getViewTreeObserver ().addOnScrollChangedListener (onScrollChangedListener );
1868
1887
// The label is attached on the Overlay relative to the content.
1869
1888
for (TooltipDrawable label : labels ) {
1870
1889
attachLabelToContentView (label );
@@ -1885,7 +1904,7 @@ protected void onDetachedFromWindow() {
1885
1904
for (TooltipDrawable label : labels ) {
1886
1905
detachLabelFromContentView (label );
1887
1906
}
1888
-
1907
+ getViewTreeObserver (). removeOnScrollChangedListener ( onScrollChangedListener );
1889
1908
super .onDetachedFromWindow ();
1890
1909
}
1891
1910
@@ -2646,10 +2665,16 @@ private String formatValue(float value) {
2646
2665
2647
2666
private void setValueForLabel (TooltipDrawable label , float value ) {
2648
2667
label .setText (formatValue (value ));
2668
+ positionLabel (label );
2669
+ ViewUtils .getContentViewOverlay (this ).add (label );
2670
+ }
2649
2671
2672
+ private void positionLabel (TooltipDrawable label ) {
2673
+ float labelValue = !TextUtils .isEmpty (label .getText ())
2674
+ ? Float .parseFloat (label .getText ().toString ()) : 0 ;
2650
2675
int left =
2651
2676
trackSidePadding
2652
- + (int ) (normalizeValue (value ) * trackWidth )
2677
+ + (int ) (normalizeValue (labelValue ) * trackWidth )
2653
2678
- label .getIntrinsicWidth () / 2 ;
2654
2679
int top = calculateTrackCenter () - (labelPadding + thumbHeight / 2 );
2655
2680
label .setBounds (left , top - label .getIntrinsicHeight (), left + label .getIntrinsicWidth (), top );
@@ -2659,8 +2684,6 @@ private void setValueForLabel(TooltipDrawable label, float value) {
2659
2684
Rect rect = new Rect (label .getBounds ());
2660
2685
DescendantOffsetUtils .offsetDescendantRect (ViewUtils .getContentView (this ), this , rect );
2661
2686
label .setBounds (rect );
2662
-
2663
- ViewUtils .getContentViewOverlay (this ).add (label );
2664
2687
}
2665
2688
2666
2689
private void invalidateTrack () {
0 commit comments