OneShotPreDrawListener

Added in 1.1.0

public final class OneShotPreDrawListener 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.

Summary

Public methods

static @NonNull OneShotPreDrawListener
add(@NonNull View view, @NonNull Runnable runnable)

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

boolean
void
void
void

Removes the listener from the ViewTreeObserver.

Public methods

add

Added in 1.1.0
public static @NonNull OneShotPreDrawListener add(@NonNull View view, @NonNull Runnable runnable)

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

Parameters
@NonNull View view

The view whose ViewTreeObserver the OnPreDrawListener should listen.

@NonNull Runnable runnable

The Runnable to execute in the OnPreDraw (once)

Returns
@NonNull OneShotPreDrawListener

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

onPreDraw

Added in 1.1.0
public boolean onPreDraw()

onViewAttachedToWindow

Added in 1.1.0
public void onViewAttachedToWindow(@NonNull View v)

onViewDetachedFromWindow

Added in 1.1.0
public void onViewDetachedFromWindow(@NonNull View v)

removeListener

Added in 1.1.0
public void removeListener()

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