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 TypeMethodDescriptionstatic OneShotPreDrawListener
Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.boolean
Callback method to be invoked when the view tree is about to be drawn.void
Called when the view is attached to a window.void
Called when the view is detached from a window.void
Removes the listener from the ViewTreeObserver.
-
Method Details
-
add
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 interfaceViewTreeObserver.OnPreDrawListener
- Returns:
- Return true to proceed with the current drawing pass, or false to cancel.
- See Also:
-
removeListener
public void removeListener()Removes the listener from the ViewTreeObserver. This is useful to call if the callback should be removed prior toonPreDraw()
. -
onViewAttachedToWindow
Description copied from interface:View.OnAttachStateChangeListener
Called when the view is attached to a window.- Specified by:
onViewAttachedToWindow
in interfaceView.OnAttachStateChangeListener
- Parameters:
v
- The view that was attached
-
onViewDetachedFromWindow
Description copied from interface:View.OnAttachStateChangeListener
Called when the view is detached from a window.- Specified by:
onViewDetachedFromWindow
in interfaceView.OnAttachStateChangeListener
- Parameters:
v
- The view that was detached
-