Package icyllis.modernui.view

# Class ViewTreeObserver

java.lang.Object
icyllis.modernui.view.ViewTreeObserver

public final class ViewTreeObserver extends Object
A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change....

A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to `View.getViewTreeObserver()` for more information.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `ViewTreeObserver.OnGlobalFocusChangeListener`
  Interface definition for a callback to be invoked when the focus state within the view tree changes.
  `static interface`
  `ViewTreeObserver.OnGlobalLayoutListener`
  Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.
  `static interface`
  `ViewTreeObserver.OnPreDrawListener`
  Interface definition for a callback to be invoked when the view tree is about to be drawn.
  `static interface`
  `ViewTreeObserver.OnScrollChangedListener`
  Interface definition for a callback to be invoked when something in the view tree has been scrolled.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addOnGlobalFocusChangeListener(ViewTreeObserver.OnGlobalFocusChangeListener listener)`
  Register a callback to be invoked when the focus state within the view tree changes.
  `void`
  `addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener)`
  Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes
  `void`
  `addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener listener)`
  Register a callback to be invoked when the view tree is about to be drawn
  `void`
  `addOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener listener)`
  Register a callback to be invoked when a view has been scrolled.
  `void`
  `dispatchOnGlobalLayout()`
  Notifies registered listeners that a global layout happened.
  `boolean`
  `dispatchOnPreDraw()`
  Notifies registered listeners that the drawing pass is about to start.
  `void`
  `dispatchOnScrollChanged()`
  Notifies registered listeners that something has scrolled.
  `boolean`
  `isAlive()`
  Indicates whether this ViewTreeObserver is alive.
  `void`
  `removeOnGlobalFocusChangeListener(ViewTreeObserver.OnGlobalFocusChangeListener victim)`
  Remove a previously installed focus change callback.
  `void`
  `removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim)`
  Remove a previously installed global layout callback
  `void`
  `removeOnPreDrawListener(ViewTreeObserver.OnPreDrawListener victim)`
  Remove a previously installed pre-draw callback
  `void`
  `removeOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener victim)`
  Remove a previously installed scroll-changed callback

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### addOnGlobalFocusChangeListener

    public void addOnGlobalFocusChangeListener(@NonNull ViewTreeObserver.OnGlobalFocusChangeListener listener)
    Register a callback to be invoked when the focus state within the view tree changes.
    Parameters:
      `listener` - The callback to add
    Throws:
      `IllegalStateException` - If `isAlive()` returns false

  - ### removeOnGlobalFocusChangeListener

    public void removeOnGlobalFocusChangeListener(@NonNull ViewTreeObserver.OnGlobalFocusChangeListener victim)
    Remove a previously installed focus change callback.
    Parameters:
      `victim` - The callback to remove
    Throws:
      `IllegalStateException` - If `isAlive()` returns false
    See Also:
      - `addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener)`

  - ### addOnGlobalLayoutListener

    public void addOnGlobalLayoutListener(@NonNull ViewTreeObserver.OnGlobalLayoutListener listener)
    Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes
    Parameters:
      `listener` - The callback to add
    Throws:
      `IllegalStateException` - If `isAlive()` returns false

  - ### removeOnGlobalLayoutListener

    public void removeOnGlobalLayoutListener(@NonNull ViewTreeObserver.OnGlobalLayoutListener victim)
    Remove a previously installed global layout callback
    Parameters:
      `victim` - The callback to remove
    Throws:
      `IllegalStateException` - If `isAlive()` returns false
    See Also:
      - `addOnGlobalLayoutListener(OnGlobalLayoutListener)`

  - ### addOnPreDrawListener

    public void addOnPreDrawListener(@NonNull ViewTreeObserver.OnPreDrawListener listener)
    Register a callback to be invoked when the view tree is about to be drawn
    Parameters:
      `listener` - The callback to add
    Throws:
      `IllegalStateException` - If `isAlive()` returns false

  - ### removeOnPreDrawListener

    public void removeOnPreDrawListener(@NonNull ViewTreeObserver.OnPreDrawListener victim)
    Remove a previously installed pre-draw callback
    Parameters:
      `victim` - The callback to remove
    Throws:
      `IllegalStateException` - If `isAlive()` returns false
    See Also:
      - `addOnPreDrawListener(OnPreDrawListener)`

  - ### addOnScrollChangedListener

    public void addOnScrollChangedListener(@NonNull ViewTreeObserver.OnScrollChangedListener listener)
    Register a callback to be invoked when a view has been scrolled.
    Parameters:
      `listener` - The callback to add
    Throws:
      `IllegalStateException` - If `isAlive()` returns false

  - ### removeOnScrollChangedListener

    public void removeOnScrollChangedListener(@NonNull ViewTreeObserver.OnScrollChangedListener victim)
    Remove a previously installed scroll-changed callback
    Parameters:
      `victim` - The callback to remove
    Throws:
      `IllegalStateException` - If `isAlive()` returns false
    See Also:
      - `addOnScrollChangedListener(OnScrollChangedListener)`

  - ### isAlive

    public boolean isAlive()
    Indicates whether this ViewTreeObserver is alive. When an observer is not alive, any call to a method (except this one) will throw an exception.

    If an application keeps a long-lived reference to this ViewTreeObserver, it should always check for the result of this method before calling any other method.

    Returns:
      True if this object is alive and be used, false otherwise.

  - ### dispatchOnGlobalLayout

    public void dispatchOnGlobalLayout()
    Notifies registered listeners that a global layout happened. This can be called manually if you are forcing a layout on a View or a hierarchy of Views that are not attached to a Window or in the GONE state.

  - ### dispatchOnPreDraw

    public boolean dispatchOnPreDraw()
    Notifies registered listeners that the drawing pass is about to start. If a listener returns true, then the drawing pass is canceled and rescheduled. This can be called manually if you are forcing the drawing on a View or a hierarchy of Views that are not attached to a Window or in the GONE state.
    Returns:
      True if the current draw should be canceled and rescheduled, false otherwise.

  - ### dispatchOnScrollChanged

    public void dispatchOnScrollChanged()
    Notifies registered listeners that something has scrolled.