ViewTreeObserverListeners

Added in 1.20.0-rc01

public final class ViewTreeObserverListeners


Summary

Public methods

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration
@MainThread
addOnDrawListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnDrawListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration
@MainThread
addOnPreDrawListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnPreDrawListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

static final @NonNull ViewTreeObserverRegistration

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Public methods

addOnDrawListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnDrawListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnDrawListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Note: ViewTreeObserver does not allow removing a draw listener during ViewTreeObserver.OnDrawListener.onDraw. Do not destroy scope or call ViewTreeObserverRegistration.remove from that callback.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnDrawListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnDrawListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnDrawListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnDrawListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Note: ViewTreeObserver does not allow removing a draw listener during ViewTreeObserver.OnDrawListener.onDraw. Do not detach scope or call ViewTreeObserverRegistration.remove from that callback.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnDrawListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnGlobalFocusChangeListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnGlobalFocusChangeListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnGlobalFocusChangeListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnGlobalFocusChangeListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnGlobalFocusChangeListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnGlobalFocusChangeListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnGlobalFocusChangeListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnGlobalFocusChangeListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnGlobalLayoutListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnGlobalLayoutListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnGlobalLayoutListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Use this when the view outlives the component that listens to it, such as a fragment listening to its activity's decor view.

  • Does nothing if scope is already destroyed.

  • Removes listener even if this view was attached to, or detached from, a window in between.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnGlobalLayoutListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnGlobalLayoutListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnGlobalLayoutListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnGlobalLayoutListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

  • Removes listener the first time scope is detached. Attaching scope again does not add listener back.

  • If scope is not attached yet, keeps listener until scope is attached and then detached.

  • Removes listener even if this view was attached to, or detached from, a window in between.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnGlobalLayoutListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnPreDrawListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnPreDrawListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnPreDrawListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnPreDrawListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnPreDrawListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnPreDrawListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnPreDrawListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnPreDrawListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnScrollChangedListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnScrollChangedListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnScrollChangedListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnScrollChangedListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnScrollChangedListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnScrollChangedListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnScrollChangedListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnScrollChangedListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnTouchModeChangeListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnTouchModeChangeListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnTouchModeChangeListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnTouchModeChangeListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnTouchModeChangeListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnTouchModeChangeListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnTouchModeChangeListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnTouchModeChangeListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnWindowAttachListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnWindowAttachListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnWindowAttachListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnWindowAttachListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnWindowAttachListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnWindowAttachListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnWindowAttachListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnWindowAttachListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached

addOnWindowFocusChangeListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnWindowFocusChangeListener(
    @NonNull View receiver,
    @NonNull LifecycleOwner scope,
    @NonNull ViewTreeObserver.OnWindowFocusChangeListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is destroyed.

Does nothing if scope is already destroyed.

Parameters
@NonNull LifecycleOwner scope

owner that removes listener when it reaches Lifecycle.State.DESTROYED

@NonNull ViewTreeObserver.OnWindowFocusChangeListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is destroyed

addOnWindowFocusChangeListener

@MainThread
public static final @NonNull ViewTreeObserverRegistration addOnWindowFocusChangeListener(
    @NonNull View receiver,
    @NonNull View scope,
    @NonNull ViewTreeObserver.OnWindowFocusChangeListener listener
)

Adds listener to this view's ViewTreeObserver and removes it when scope is detached from its window.

Attaching scope again does not add listener back.

Parameters
@NonNull View scope

view that removes listener when it is detached from its window

@NonNull ViewTreeObserver.OnWindowFocusChangeListener listener

callback to add

Returns
@NonNull ViewTreeObserverRegistration

handle that removes listener before scope is detached