mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
[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
This commit is contained in:
parent
391d1f8ac5
commit
89a1bf883f
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<B extends BaseTransientBottomBar<B>
|
||||
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<B extends BaseTransientBottomBar<B>
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user