2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.internal.view;
18 import android.view.View;
19 import android.view.ViewTreeObserver;
22 * An OnPreDrawListener that will remove itself after one OnPreDraw call. Typical
25 * OneShotPreDrawListener.add(view, () -> { view.doSomething(); })
28 * The listener will also remove itself from the ViewTreeObserver when the view
29 * is detached from the view hierarchy. In that case, the Runnable will never be
32 public class OneShotPreDrawListener implements ViewTreeObserver.OnPreDrawListener,
33 View.OnAttachStateChangeListener {
34 private final View mView;
35 private ViewTreeObserver mViewTreeObserver;
36 private final Runnable mRunnable;
37 private final boolean mReturnValue;
39 private OneShotPreDrawListener(View view, boolean returnValue, Runnable runnable) {
41 mViewTreeObserver = view.getViewTreeObserver();
43 mReturnValue = returnValue;
47 * Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver. The
48 * return value from the OnPreDrawListener is {@code true}.
50 * @param view The view whose ViewTreeObserver the OnPreDrawListener should listen.
51 * @param runnable The Runnable to execute in the OnPreDraw (once)
52 * @return The added OneShotPreDrawListener. It can be removed prior to
53 * the onPreDraw by calling {@link #removeListener()}.
55 public static OneShotPreDrawListener add(View view, Runnable runnable) {
56 return add(view, true, runnable);
60 * Creates a OneShotPreDrawListener and adds it to view's ViewTreeObserver.
62 * @param view The view whose ViewTreeObserver the OnPreDrawListener should listen.
63 * @param returnValue The value to be returned from the OnPreDrawListener.
64 * @param runnable The Runnable to execute in the OnPreDraw (once)
65 * @return The added OneShotPreDrawListener. It can be removed prior to
66 * the onPreDraw by calling {@link #removeListener()}.
68 public static OneShotPreDrawListener add(View view, boolean returnValue, Runnable runnable) {
69 OneShotPreDrawListener listener = new OneShotPreDrawListener(view, returnValue, runnable);
70 view.getViewTreeObserver().addOnPreDrawListener(listener);
71 view.addOnAttachStateChangeListener(listener);
76 public boolean onPreDraw() {
83 * Removes the listener from the ViewTreeObserver. This is useful to call if the
84 * callback should be removed prior to {@link #onPreDraw()}.
86 public void removeListener() {
87 if (mViewTreeObserver.isAlive()) {
88 mViewTreeObserver.removeOnPreDrawListener(this);
90 mView.getViewTreeObserver().removeOnPreDrawListener(this);
92 mView.removeOnAttachStateChangeListener(this);
96 public void onViewAttachedToWindow(View v) {
97 mViewTreeObserver = v.getViewTreeObserver();
101 public void onViewDetachedFromWindow(View v) {