Register now for Android Dev Summit 2019!

OneShotPreDrawListener

public final class OneShotPreDrawListener
extends Object implements ViewTreeObserver.OnPreDrawListener, View.OnAttachStateChangeListener

java.lang.Object
   ↳ androidx.core.view.OneShotPreDrawListener


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.

Summary

Public methods

static OneShotPreDrawListener add(View view, Runnable runnable)

Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.

boolean onPreDraw()
void onViewAttachedToWindow(View v)
void onViewDetachedFromWindow(View v)
void removeListener()

Removes the listener from the ViewTreeObserver.

Inherited methods

Public methods

add

public static OneShotPreDrawListener add (View view, 
                Runnable runnable)

Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.

Parameters
view View: The view whose ViewTreeObserver the OnPreDrawListener should listen.

runnable Runnable: The Runnable to execute in the OnPreDraw (once)

Returns
OneShotPreDrawListener The added OneShotPreDrawListener. It can be removed prior to the onPreDraw by calling removeListener().

onPreDraw

public boolean onPreDraw ()

Returns
boolean

onViewAttachedToWindow

public void onViewAttachedToWindow (View v)

Parameters
v View

onViewDetachedFromWindow

public void onViewDetachedFromWindow (View v)

Parameters
v View

removeListener

public void removeListener ()

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