OneShotPreDrawListener
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 |
Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver. |
boolean |
|
void |
|
void |
|
void |
Removes the listener from the ViewTreeObserver. |
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
public void removeListener()
Removes the listener from the ViewTreeObserver. This is useful to call if the callback should be removed prior to onPreDraw.