From 89a1bf883fa614702bb43d2cfa75a38005a42421 Mon Sep 17 00:00:00 2001 From: dniz Date: Wed, 23 Sep 2020 08:41:56 -0700 Subject: [PATCH] [Snackbar] Added ability to have Snackbar position respond to anchor view layout changes Disabled by default to avoid changes in behavior and breakages in tests PiperOrigin-RevId: 333299318 --- .../android/material/internal/ViewUtils.java | 27 ++++++++++++ .../snackbar/BaseTransientBottomBar.java | 41 +++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/java/com/google/android/material/internal/ViewUtils.java b/lib/java/com/google/android/material/internal/ViewUtils.java index 894134c3e..d283cb15a 100644 --- a/lib/java/com/google/android/material/internal/ViewUtils.java +++ b/lib/java/com/google/android/material/internal/ViewUtils.java @@ -25,6 +25,8 @@ import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.PorterDuff; import android.os.Build; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import android.util.AttributeSet; @@ -33,6 +35,8 @@ import android.view.View; import android.view.View.OnAttachStateChangeListener; import android.view.ViewGroup; import android.view.ViewParent; +import android.view.ViewTreeObserver; +import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.WindowInsets; import android.view.inputmethod.InputMethodManager; import androidx.annotation.Dimension; @@ -312,4 +316,27 @@ public class ViewUtils { public static ViewOverlayImpl getContentViewOverlay(@NonNull View view) { return getOverlay(getContentView(view)); } + + public static void addOnGlobalLayoutListener( + @Nullable View view, @NonNull OnGlobalLayoutListener victim) { + if (view != null) { + view.getViewTreeObserver().addOnGlobalLayoutListener(victim); + } + } + + public static void removeOnGlobalLayoutListener( + @Nullable View view, @NonNull OnGlobalLayoutListener victim) { + if (view != null) { + removeOnGlobalLayoutListener(view.getViewTreeObserver(), victim); + } + } + + public static void removeOnGlobalLayoutListener( + @NonNull ViewTreeObserver viewTreeObserver, @NonNull OnGlobalLayoutListener victim) { + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { + viewTreeObserver.removeOnGlobalLayoutListener(victim); + } else { + viewTreeObserver.removeGlobalOnLayoutListener(victim); + } + } } diff --git a/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java b/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java index 1a4562765..7b6ec2473 100644 --- a/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java +++ b/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java @@ -62,6 +62,7 @@ import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.MarginLayoutParams; import android.view.ViewParent; +import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.WindowInsets; import android.view.WindowManager; import android.view.accessibility.AccessibilityManager; @@ -262,6 +263,18 @@ public abstract class BaseTransientBottomBar private int duration; private boolean gestureInsetBottomIgnored; @Nullable private View anchorView; + private boolean anchorViewLayoutListenerEnabled = false; + private final OnGlobalLayoutListener anchorViewLayoutListener = + new OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + if (!anchorViewLayoutListenerEnabled) { + return; + } + extraBottomMarginAnchorView = calculateBottomMarginForAnchorView(); + updateMargins(); + } + }; @RequiresApi(VERSION_CODES.Q) private final Runnable bottomMarginGestureInsetRunnable = @@ -549,22 +562,42 @@ public abstract class BaseTransientBottomBar /** Sets the view the {@link BaseTransientBottomBar} should be anchored above. */ @NonNull public B setAnchorView(@Nullable View anchorView) { + ViewUtils.removeOnGlobalLayoutListener(this.anchorView, anchorViewLayoutListener); this.anchorView = anchorView; + ViewUtils.addOnGlobalLayoutListener(this.anchorView, anchorViewLayoutListener); return (B) this; } /** - * Sets the id of the view the {@link BaseTransientBottomBar} should be anchored above. + * Sets the view the {@link BaseTransientBottomBar} should be anchored above by id. * * @throws IllegalArgumentException if the anchor view is not found. */ @NonNull public B setAnchorView(@IdRes int anchorViewId) { - this.anchorView = targetParent.findViewById(anchorViewId); - if (this.anchorView == null) { + View anchorView = targetParent.findViewById(anchorViewId); + if (anchorView == null) { throw new IllegalArgumentException("Unable to find anchor view with id: " + anchorViewId); } - return (B) this; + return setAnchorView(anchorView); + } + + /** + * Returns whether the anchor view layout listener is enabled. + * + * @see #setAnchorViewLayoutListenerEnabled(boolean) + */ + public boolean isAnchorViewLayoutListenerEnabled() { + return anchorViewLayoutListenerEnabled; + } + + /** + * Sets whether the anchor view layout listener is enabled. If enabled, the {@link + * BaseTransientBottomBar} will recalculate and update its position when the position of the + * anchor view is changed. + */ + public void setAnchorViewLayoutListenerEnabled(boolean anchorViewLayoutListenerEnabled) { + this.anchorViewLayoutListenerEnabled = anchorViewLayoutListenerEnabled; } /**