Package icyllis.modernui.view

# Class OneShotPreDrawListener

java.lang.Object
icyllis.modernui.view.OneShotPreDrawListener

All Implemented Interfaces:
  `View.OnAttachStateChangeListener`, `ViewTreeObserver.OnPreDrawListener`

public final class OneShotPreDrawListener extends Object implements ViewTreeObserver.OnPreDrawListener, View.OnAttachStateChangeListener
An OnPreDrawListener that will remove itself after one OnPreDraw call. Typical usage is:

         OneShotPreDrawListener.add(view, () -> view.doSomething())

The onPreDraw always returns true.

The listener will also remove itself from the ViewTreeObserver when the view is detached from the view hierarchy. In that case, the Runnable will never be executed.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static OneShotPreDrawListener`
  `add(View view, Runnable runnable)`
  Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.
  `boolean`
  `onPreDraw()`
  Callback method to be invoked when the view tree is about to be drawn.
  `void`
  `onViewAttachedToWindow(View v)`
  Called when the view is attached to a window.
  `void`
  `onViewDetachedFromWindow(View v)`
  Called when the view is detached from a window.
  `void`
  `removeListener()`
  Removes the listener from the ViewTreeObserver.

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

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

- ## Method Details

  - ### add

    @NonNull public static OneShotPreDrawListener add(@NonNull View view, @NonNull Runnable runnable)
    Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.
    Parameters:
      `view` - The view whose ViewTreeObserver the OnPreDrawListener should listen.
      `runnable` - The Runnable to execute in the OnPreDraw (once)
    Returns:
      The added OneShotPreDrawListener. It can be removed prior to the onPreDraw by calling `removeListener()`.

  - ### onPreDraw

    public boolean onPreDraw()
    Description copied from interface: `ViewTreeObserver.OnPreDrawListener`
    Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.
    Specified by:
      `onPreDraw` in interface `ViewTreeObserver.OnPreDrawListener`
    Returns:
      Return true to proceed with the current drawing pass, or false to cancel.
    See Also:
      - `View.onMeasure(int, int)`
      - `View.onLayout(boolean, int, int, int, int)`
      - `View.onDraw(icyllis.modernui.graphics.Canvas)`

  - ### removeListener

    public void removeListener()
    Removes the listener from the ViewTreeObserver. This is useful to call if the callback should be removed prior to `onPreDraw()`.

  - ### onViewAttachedToWindow

    public void onViewAttachedToWindow(@NonNull View v)
    Description copied from interface: `View.OnAttachStateChangeListener`
    Called when the view is attached to a window.
    Specified by:
      `onViewAttachedToWindow` in interface `View.OnAttachStateChangeListener`
    Parameters:
      `v` - The view that was attached

  - ### onViewDetachedFromWindow

    public void onViewDetachedFromWindow(@NonNull View v)
    Description copied from interface: `View.OnAttachStateChangeListener`
    Called when the view is detached from a window.
    Specified by:
      `onViewDetachedFromWindow` in interface `View.OnAttachStateChangeListener`
    Parameters:
      `v` - The view that was detached