Skip to content

Commit 5e5eee0

Browse files
paulfthomasdsn5ft
authored andcommitted
[Slider] Fix slider label not moving while scrolling
Resolves #3660 Resolves #2869 Resolves #3665 PiperOrigin-RevId: 581318308
1 parent 4246672 commit 5e5eee0

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import android.os.Parcel;
5858
import android.os.Parcelable;
5959
import androidx.appcompat.content.res.AppCompatResources;
60+
import android.text.TextUtils;
6061
import android.util.AttributeSet;
6162
import android.util.Log;
6263
import android.view.KeyEvent;
@@ -65,6 +66,7 @@
6566
import android.view.ViewConfiguration;
6667
import android.view.ViewGroup;
6768
import android.view.ViewParent;
69+
import android.view.ViewTreeObserver;
6870
import android.view.accessibility.AccessibilityEvent;
6971
import android.view.accessibility.AccessibilityManager;
7072
import android.widget.SeekBar;
@@ -338,6 +340,22 @@ abstract class BaseSlider<
338340
private float touchPosition;
339341
@SeparationUnit private int separationUnit = UNIT_PX;
340342

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+
341359
/**
342360
* Determines the behavior of the label which can be any of the following.
343361
*
@@ -1865,6 +1883,7 @@ public void setEnabled(boolean enabled) {
18651883
@Override
18661884
protected void onAttachedToWindow() {
18671885
super.onAttachedToWindow();
1886+
getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener);
18681887
// The label is attached on the Overlay relative to the content.
18691888
for (TooltipDrawable label : labels) {
18701889
attachLabelToContentView(label);
@@ -1885,7 +1904,7 @@ protected void onDetachedFromWindow() {
18851904
for (TooltipDrawable label : labels) {
18861905
detachLabelFromContentView(label);
18871906
}
1888-
1907+
getViewTreeObserver().removeOnScrollChangedListener(onScrollChangedListener);
18891908
super.onDetachedFromWindow();
18901909
}
18911910

@@ -2646,10 +2665,16 @@ private String formatValue(float value) {
26462665

26472666
private void setValueForLabel(TooltipDrawable label, float value) {
26482667
label.setText(formatValue(value));
2668+
positionLabel(label);
2669+
ViewUtils.getContentViewOverlay(this).add(label);
2670+
}
26492671

2672+
private void positionLabel(TooltipDrawable label) {
2673+
float labelValue = !TextUtils.isEmpty(label.getText())
2674+
? Float.parseFloat(label.getText().toString()) : 0;
26502675
int left =
26512676
trackSidePadding
2652-
+ (int) (normalizeValue(value) * trackWidth)
2677+
+ (int) (normalizeValue(labelValue) * trackWidth)
26532678
- label.getIntrinsicWidth() / 2;
26542679
int top = calculateTrackCenter() - (labelPadding + thumbHeight / 2);
26552680
label.setBounds(left, top - label.getIntrinsicHeight(), left + label.getIntrinsicWidth(), top);
@@ -2659,8 +2684,6 @@ private void setValueForLabel(TooltipDrawable label, float value) {
26592684
Rect rect = new Rect(label.getBounds());
26602685
DescendantOffsetUtils.offsetDescendantRect(ViewUtils.getContentView(this), this, rect);
26612686
label.setBounds(rect);
2662-
2663-
ViewUtils.getContentViewOverlay(this).add(label);
26642687
}
26652688

26662689
private void invalidateTrack() {

0 commit comments

Comments
 (0)