OneShotPreDrawListener

public final class 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 constructors

Public methods

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

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

@Override boolean
@Override void
@Override void
void

Removes the listener from the ViewTreeObserver.

Public constructors

OneShotPreDrawListener

public final OneShotPreDrawListener()

Public methods

add

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

@Override
public boolean onPreDraw()

onViewAttachedToWindow

@Override
public void onViewAttachedToWindow(@NonNull View v)

onViewDetachedFromWindow

@Override
public void onViewDetachedFromWindow(@NonNull View v)

removeListener

public void removeListener()

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