@@ -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