@@ -370,6 +370,8 @@ abstract class BaseSlider<
370
370
invalidate ();
371
371
};
372
372
373
+ private boolean thisAndAncestorsVisible ;
374
+
373
375
/**
374
376
* Determines the behavior of the label which can be any of the following.
375
377
*
@@ -406,6 +408,9 @@ public BaseSlider(
406
408
// Ensure we are using the correctly themed context rather than the context that was passed in.
407
409
context = getContext ();
408
410
411
+ // Initialize with just this view's visibility.
412
+ thisAndAncestorsVisible = isShown ();
413
+
409
414
inactiveTrackPaint = new Paint ();
410
415
activeTrackPaint = new Paint ();
411
416
@@ -1914,6 +1919,10 @@ public void setEnabled(boolean enabled) {
1914
1919
@ Override
1915
1920
protected void onAttachedToWindow () {
1916
1921
super .onAttachedToWindow ();
1922
+
1923
+ // Update factoring in the visibility of all ancestors.
1924
+ thisAndAncestorsVisible = isShown ();
1925
+
1917
1926
getViewTreeObserver ().addOnScrollChangedListener (onScrollChangedListener );
1918
1927
getViewTreeObserver ().addOnGlobalLayoutListener (onGlobalLayoutListener );
1919
1928
// The label is attached on the Overlay relative to the content.
@@ -2705,7 +2714,19 @@ private void updateLabels() {
2705
2714
private boolean isSliderVisibleOnScreen () {
2706
2715
final Rect contentViewBounds = new Rect ();
2707
2716
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 ;
2709
2730
}
2710
2731
2711
2732
private void ensureLabelsRemoved () {
0 commit comments