Skip to content

Commit 9bf5edd

Browse files
manabu-nakamurapekingme
authored andcommitted
[Slider] Make sure label is removed with slider is hidden
Resolves #4319 Resolves #4320 GIT_ORIGIN_REV_ID=6b96f2d40b77900b3479e3af61d3fa6f7c3e7c9d PiperOrigin-RevId: 684473315
1 parent 6bacb5a commit 9bf5edd

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ abstract class BaseSlider<
370370
invalidate();
371371
};
372372

373+
private boolean thisAndAncestorsVisible;
374+
373375
/**
374376
* Determines the behavior of the label which can be any of the following.
375377
*
@@ -406,6 +408,9 @@ public BaseSlider(
406408
// Ensure we are using the correctly themed context rather than the context that was passed in.
407409
context = getContext();
408410

411+
// Initialize with just this view's visibility.
412+
thisAndAncestorsVisible = isShown();
413+
409414
inactiveTrackPaint = new Paint();
410415
activeTrackPaint = new Paint();
411416

@@ -1914,6 +1919,10 @@ public void setEnabled(boolean enabled) {
19141919
@Override
19151920
protected void onAttachedToWindow() {
19161921
super.onAttachedToWindow();
1922+
1923+
// Update factoring in the visibility of all ancestors.
1924+
thisAndAncestorsVisible = isShown();
1925+
19171926
getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener);
19181927
getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
19191928
// The label is attached on the Overlay relative to the content.
@@ -2705,7 +2714,19 @@ private void updateLabels() {
27052714
private boolean isSliderVisibleOnScreen() {
27062715
final Rect contentViewBounds = new Rect();
27072716
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
2708-
return getLocalVisibleRect(contentViewBounds);
2717+
return getLocalVisibleRect(contentViewBounds) && isThisAndAncestorsVisible();
2718+
}
2719+
2720+
private boolean isThisAndAncestorsVisible() {
2721+
// onVisibilityAggregated is only available on N+ devices, so on pre-N devices we check if this
2722+
// view and its ancestors are visible each time, in case one of the visibilities has changed.
2723+
return (VERSION.SDK_INT >= VERSION_CODES.N) ? thisAndAncestorsVisible : isShown();
2724+
}
2725+
2726+
@Override
2727+
public void onVisibilityAggregated(boolean isVisible) {
2728+
super.onVisibilityAggregated(isVisible);
2729+
this.thisAndAncestorsVisible = isVisible;
27092730
}
27102731

27112732
private void ensureLabelsRemoved() {

0 commit comments

Comments
 (0)