监听View树ViewTreeObserver深度剖析
ViewTreeObserver用来监听View视图树的变化,正如它的注释所说:
/**
* A view tree observer is used to register listeners that can be notified of global
* changes in the view tree. Such global events include, but are not limited to,
* layout of the whole tree, beginning of the drawing pass, touch mode change....
*
* A ViewTreeObserver should never be instantiated by applications as it is provided
* by the views hierarchy. Refer to {@link android.view.View#getViewTreeObserver()}
* for more information.
*/
public final class ViewTreeObserver {
...
}ViewTreeObserver类是final类不可继承,且构造函数默认View包可见。应用只能通过getViewTreeObserver()方法获取:
/**
* Returns the ViewTreeObserver for this view's hierarchy. The view tree
* observer can be used to get notifications when global events, like
* layout, happen.
*
* The returned ViewTreeObserver observer is not guaranteed to remain
* valid for the lifetime of this View. If the caller of this method keeps
* a long-lived reference to ViewTreeObserver, it should always check for
* the return value of {@link ViewTreeObserver#isAlive()}.
*
* @return The ViewTreeObserver for this view's hierarchy.
*/
public ViewTreeObserver getViewTreeObserver() {
if (mAttachInfo != null) {
return mAttachInfo.mTreeObserver;
}
if (mFloatingTreeObserver == null) {
mFloatingTreeObserver = new ViewTreeObserver(mContext);
}
return mFloatingTreeObserver;
}在ViewRootImpl构造的时候,同时也会构造一个AttachInfo,在这mAttachInfo中默认就拥有一个mTreeObserver实例。使用前最好先通过isAlive()方法判断是否可用:
/**
* Indicates whether this ViewTreeObserver is alive. When an observer is not alive,
* any call to a method (except this one) will throw an exception.
*
* If an application keeps a long-lived reference to this ViewTreeObserver, it should
* always check for the result of this method before calling any other method.
*
* @return True if this object is alive and be used, false otherwise.
*/
public boolean isAlive() {
return mAlive;
}当一个ViewTreeObserver被kill(),此时再在这个视图树观察者对象上调用任何方法:如注册或移除监听等操作将会抛出移除。这时候仅isAlive()和kill()方法不会抛出异常。
/**
* Marks this ViewTreeObserver as not alive. After invoking this method, invoking
* any other method but {@link #isAlive()} and {@link #kill()} will throw an Exception.
*
* @hide
*/
private void kill() {
mAlive = false;
}1、11种监听器
ViewTreeObserver中定义了十一种监听器,全方位对View进行监听。其中三种监听器已经@hide不再对应用层提供:OnWindowShownListener、OnComputeInternalInsetsListener、OnEnterAnimationCompleteListener。这倒难不倒开发者,反射。
public interface OnWindowAttachListener public interface OnWindowFocusChangeListener public interface OnGlobalFocusChangeListener public interface OnGlobalLayoutListener public interface OnPreDrawListener public interface OnDrawListener public interface OnTouchModeChangeListener public interface OnScrollChangedListener public interface OnWindowShownListener @hide public interface OnComputeInternalInsetsListener @hide public interface OnEnterAnimationCompleteListener @hide
①OnWindowAttachListener:
/**
* Interface definition for a callback to be invoked when the view hierarchy is
* attached to and detached from its window.
*/
public interface OnWindowAttachListener {
/**
* Callback method to be invoked when the view hierarchy is attached to a window
*/
public void onWindowAttached();
/**
* Callback method to be invoked when the view hierarchy is detached from a window
*/
public void onWindowDetached();
}②OnWindowFocusChangeListener:
/**
* Interface definition for a callback to be invoked when the view hierarchy's window
* focus state changes.
*/
public interface OnWindowFocusChangeListener {
/**
* Callback method to be invoked when the window focus changes in the view tree.
*
* @param hasFocus Set to true if the window is gaining focus, false if it is
* losing focus.
*/
public void onWindowFocusChanged(boolean hasFocus);
}③OnGlobalFocusChangeListener:
/**
* Interface definition for a callback to be invoked when the focus state within
* the view tree changes.
*/
public interface OnGlobalFocusChangeListener {
/**
* Callback method to be invoked when the focus changes in the view tree. When
* the view tree transitions from touch mode to non-touch mode, oldFocus is null.
* When the view tree transitions from non-touch mode to touch mode, newFocus is
* null. When focus changes in non-touch mode (without transition from or to
* touch mode) either oldFocus or newFocus can be null.
*
* @param oldFocus The previously focused view, if any.
* @param newFocus The newly focused View, if any.
*/
public void onGlobalFocusChanged(View oldFocus, View newFocus);
}④OnGlobalLayoutListener:
/**
* Interface definition for a callback to be invoked when the global layout state
* or the visibility of views within the view tree changes.
*/
public interface OnGlobalLayoutListener {
/**
* Callback method to be invoked when the global layout state or the visibility of views
* within the view tree changes
*/
public void onGlobalLayout();
}⑤OnPreDrawListener:
/**
* Interface definition for a callback to be invoked when the view tree is about to be drawn.
*/
public interface OnPreDrawListener {
/**
* Callback method to be invoked when the view tree is about to be drawn. At this point, all
* views in the tree have been measured and given a frame. Clients can use this to adjust
* their scroll bounds or even to request a new layout before drawing occurs.
*
* @return Return true to proceed with the current drawing pass, or false to cancel.
*
* @see android.view.View#onMeasure
* @see android.view.View#onLayout
* @see android.view.View#onDraw
*/
public boolean onPreDraw();
}⑥OnDrawListener:
/**
* Interface definition for a callback to be invoked when the view tree is about to be drawn.
*/
public interface OnDrawListener {
/**
* <p>Callback method to be invoked when the view tree is about to be drawn. At this point,
* views cannot be modified in any way.</p>
*
* <p>Unlike with {@link OnPreDrawListener}, this method cannot be used to cancel the
* current drawing pass.</p>
*
* <p>An {@link OnDrawListener} listener <strong>cannot be added or removed</strong>
* from this method.</p>
*
* @see android.view.View#onMeasure
* @see android.view.View#onLayout
* @see android.view.View#onDraw
*/
public void onDraw();
}⑦OnTouchModeChangeListener:
/**
* Interface definition for a callback to be invoked when the touch mode changes.
*/
public interface OnTouchModeChangeListener {
/**
* Callback method to be invoked when the touch mode changes.
*
* @param isInTouchMode True if the view hierarchy is now in touch mode, false otherwise.
*/
public void onTouchModeChanged(boolean isInTouchMode);
}⑧OnScrollChangedListener:
/**
* Interface definition for a callback to be invoked when
* something in the view tree has been scrolled.
*/
public interface OnScrollChangedListener {
/**
* Callback method to be invoked when something in the view tree
* has been scrolled.
*/
public void onScrollChanged();
}⑨OnWindowShownListener @hide:
/**
* Interface definition for a callback noting when a system window has been displayed.
* This is only used for non-Activity windows. Activity windows can use
* Activity.onEnterAnimationComplete() to get the same signal.
* @hide
*/
public interface OnWindowShownListener {
/**
* Callback method to be invoked when a non-activity window is fully shown.
*/
void onWindowShown();
}⑩OnComputeInternalInsetsListener @hide:
/**
* Interface definition for a callback to be invoked when layout has
* completed and the client can compute its interior insets.
*
* We are not yet ready to commit to this API and support it, so
* @hide
*/
public interface OnComputeInternalInsetsListener {
/**
* Callback method to be invoked when layout has completed and the
* client can compute its interior insets.
*
* @param inoutInfo Should be filled in by the implementation with
* the information about the insets of the window. This is called
* with whatever values the previous OnComputeInternalInsetsListener
* returned, if there are multiple such listeners in the window.
*/
public void onComputeInternalInsets(InternalInsetsInfo inoutInfo);
}⑪OnEnterAnimationCompleteListener @hide:
/**
* @hide
*/
public interface OnEnterAnimationCompleteListener {
public void onEnterAnimationComplete();
}
2、ViewTreeObserver注册/移除监听器
ViewTreeObserver正如其名字,是观察者模式的典型使用场景。十一种监听器用到了11个集合,分别存储注册的观察者。
private CopyOnWriteArrayList<OnWindowFocusChangeListener> mOnWindowFocusListeners; private CopyOnWriteArrayList<OnWindowAttachListener> mOnWindowAttachListeners; private CopyOnWriteArrayList<OnGlobalFocusChangeListener> mOnGlobalFocusListeners; private CopyOnWriteArrayList<OnTouchModeChangeListener> mOnTouchModeChangeListeners; @UnsupportedAppUsage private CopyOnWriteArrayList<OnEnterAnimationCompleteListener> mOnEnterAnimationCompleteListeners; private CopyOnWriteArray<OnGlobalLayoutListener> mOnGlobalLayoutListeners; @UnsupportedAppUsage(maxTargetSdk = R) private CopyOnWriteArray<OnComputeInternalInsetsListener> mOnComputeInternalInsetsListeners; @UnsupportedAppUsage private CopyOnWriteArray<OnScrollChangedListener> mOnScrollChangedListeners; @UnsupportedAppUsage private CopyOnWriteArray<OnPreDrawListener> mOnPreDrawListeners; private CopyOnWriteArray<OnWindowShownListener> mOnWindowShownListeners; private ArrayList<OnDrawListener> mOnDrawListeners;
向ViewTreeObserver注册观察者也就行将定义的观察者对象放到对应的集合中。每种类型的观察者都提供了一对方法用来注册和移除观察者。所有监听器的注册和移除都会检查checkIsAlive(),否则会抛出异常:This ViewTreeObserver is not alive。
private void checkIsAlive() {
if (!mAlive) {
throw new IllegalStateException("This ViewTreeObserver is not alive, call "
+ "getViewTreeObserver() again");
}
}2.1、OnWindowFocusChangeListener监听器注册/移除
注册
/**
* Register a callback to be invoked when the window focus state within the view tree changes.
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnWindowFocusChangeListener(OnWindowFocusChangeListener listener) {
checkIsAlive();
if (mOnWindowFocusListeners == null) {
mOnWindowFocusListeners
= new CopyOnWriteArrayList<OnWindowFocusChangeListener>();
}
mOnWindowFocusListeners.add(listener);
}移除
/**
* Remove a previously installed window focus change callback.
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnWindowFocusChangeListener(android.view.ViewTreeObserver.OnWindowFocusChangeListener)
*/
public void removeOnWindowFocusChangeListener(OnWindowFocusChangeListener victim) {
checkIsAlive();
if (mOnWindowFocusListeners == null) {
return;
}
mOnWindowFocusListeners.remove(victim);
}
2.2、OnWindowAttachListener监听器注册/移除
注册
/**
* Register a callback to be invoked when the view hierarchy is attached to a window.
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnWindowAttachListener(OnWindowAttachListener listener) {
checkIsAlive();
if (mOnWindowAttachListeners == null) {
mOnWindowAttachListeners
= new CopyOnWriteArrayList<OnWindowAttachListener>();
}
mOnWindowAttachListeners.add(listener);
}移除
/**
* Remove a previously installed window attach callback.
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnWindowAttachListener(android.view.ViewTreeObserver.OnWindowAttachListener)
*/
public void removeOnWindowAttachListener(OnWindowAttachListener victim) {
checkIsAlive();
if (mOnWindowAttachListeners == null) {
return;
}
mOnWindowAttachListeners.remove(victim);
}
2.3、OnGlobalFocusChangeListener监听器注册/移除
注册
/*
* Register a callback to be invoked when the focus state within the view tree changes.
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener listener) {
checkIsAlive();
if (mOnGlobalFocusListeners == null) {
mOnGlobalFocusListeners = new CopyOnWriteArrayList<OnGlobalFocusChangeListener>();
}
mOnGlobalFocusListeners.add(listener);
}移除
/**
* Remove a previously installed focus change callback.
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener)
*/
public void removeOnGlobalFocusChangeListener(OnGlobalFocusChangeListener victim) {
checkIsAlive();
if (mOnGlobalFocusListeners == null) {
return;
}
mOnGlobalFocusListeners.remove(victim);
}
2.4、OnTouchModeChangeListener监听器注册/移除
注册
/**
* Register a callback to be invoked when the invoked when the touch mode changes.
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnTouchModeChangeListener(OnTouchModeChangeListener listener) {
checkIsAlive();
if (mOnTouchModeChangeListeners == null) {
mOnTouchModeChangeListeners = new CopyOnWriteArrayList<OnTouchModeChangeListener>();
}
mOnTouchModeChangeListeners.add(listener);
}移除
/**
* Remove a previously installed touch mode change callback
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnTouchModeChangeListener(OnTouchModeChangeListener)
*/
public void removeOnTouchModeChangeListener(OnTouchModeChangeListener victim) {
checkIsAlive();
if (mOnTouchModeChangeListeners == null) {
return;
}
mOnTouchModeChangeListeners.remove(victim);
}
2.5、OnEnterAnimationCompleteListener监听器注册/移除 @hide
注册
/**
* @hide
*/
public void addOnEnterAnimationCompleteListener(OnEnterAnimationCompleteListener listener) {
checkIsAlive();
if (mOnEnterAnimationCompleteListeners == null) {
mOnEnterAnimationCompleteListeners =
new CopyOnWriteArrayList<OnEnterAnimationCompleteListener>();
}
mOnEnterAnimationCompleteListeners.add(listener);
}移除
/**
* @hide
*/
public void removeOnEnterAnimationCompleteListener(OnEnterAnimationCompleteListener listener) {
checkIsAlive();
if (mOnEnterAnimationCompleteListeners == null) {
return;
}
mOnEnterAnimationCompleteListeners.remove(listener);
}
2.6、OnGlobalLayoutListener监听器注册/移除
注册
/**
* Register a callback to be invoked when the global layout state or the visibility of views
* within the view tree changes
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnGlobalLayoutListener(OnGlobalLayoutListener listener) {
checkIsAlive();
if (mOnGlobalLayoutListeners == null) {
mOnGlobalLayoutListeners = new CopyOnWriteArray<OnGlobalLayoutListener>();
}
mOnGlobalLayoutListeners.add(listener);
}移除
/**
* Remove a previously installed global layout callback
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnGlobalLayoutListener(OnGlobalLayoutListener)
*/
public void removeOnGlobalLayoutListener(OnGlobalLayoutListener victim) {
checkIsAlive();
if (mOnGlobalLayoutListeners == null) {
return;
}
mOnGlobalLayoutListeners.remove(victim);
}
2.7、OnComputeInternalInsetsListener监听器注册/移除 @hide
注册
/**
* Register a callback to be invoked when the invoked when it is time to
* compute the window's internal insets.
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* We are not yet ready to commit to this API and support it, so
* @hide
*/
@UnsupportedAppUsage
public void addOnComputeInternalInsetsListener(OnComputeInternalInsetsListener listener) {
checkIsAlive();
if (mOnComputeInternalInsetsListeners == null) {
mOnComputeInternalInsetsListeners =
new CopyOnWriteArray<OnComputeInternalInsetsListener>();
}
mOnComputeInternalInsetsListeners.add(listener);
}移除
/**
* Remove a previously installed internal insets computation callback
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnComputeInternalInsetsListener(OnComputeInternalInsetsListener)
*
* We are not yet ready to commit to this API and support it, so
* @hide
*/
@UnsupportedAppUsage
public void removeOnComputeInternalInsetsListener(OnComputeInternalInsetsListener victim) {
checkIsAlive();
if (mOnComputeInternalInsetsListeners == null) {
return;
}
mOnComputeInternalInsetsListeners.remove(victim);
}
2.8、OnScrollChangedListener监听器注册/移除
注册
/**
* Register a callback to be invoked when a view has been scrolled.
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnScrollChangedListener(OnScrollChangedListener listener) {
checkIsAlive();
if (mOnScrollChangedListeners == null) {
mOnScrollChangedListeners = new CopyOnWriteArray<OnScrollChangedListener>();
}
mOnScrollChangedListeners.add(listener);
}移除
/**
* Remove a previously installed scroll-changed callback
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnScrollChangedListener(OnScrollChangedListener)
*/
public void removeOnScrollChangedListener(OnScrollChangedListener victim) {
checkIsAlive();
if (mOnScrollChangedListeners == null) {
return;
}
mOnScrollChangedListeners.remove(victim);
}
2.9、OnPreDrawListener监听器注册/移除
注册
/**
* Register a callback to be invoked when the view tree is about to be drawn
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnPreDrawListener(OnPreDrawListener listener) {
checkIsAlive();
if (mOnPreDrawListeners == null) {
mOnPreDrawListeners = new CopyOnWriteArray<OnPreDrawListener>();
}
mOnPreDrawListeners.add(listener);
}移除
/**
* Remove a previously installed pre-draw callback
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnPreDrawListener(OnPreDrawListener)
*/
public void removeOnPreDrawListener(OnPreDrawListener victim) {
checkIsAlive();
if (mOnPreDrawListeners == null) {
return;
}
mOnPreDrawListeners.remove(victim);
}
2.10、OnWindowShownListener监听器注册/移除 @hide
注册
/**
* Register a callback to be invoked when the view tree window has been shown
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
* @hide
*/
public void addOnWindowShownListener(OnWindowShownListener listener) {
checkIsAlive();
if (mOnWindowShownListeners == null) {
mOnWindowShownListeners = new CopyOnWriteArray<OnWindowShownListener>();
}
mOnWindowShownListeners.add(listener);
if (mWindowShown) {
listener.onWindowShown();
}
}移除
/**
* Remove a previously installed window shown callback
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnWindowShownListener(OnWindowShownListener)
* @hide
*/
public void removeOnWindowShownListener(OnWindowShownListener victim) {
checkIsAlive();
if (mOnWindowShownListeners == null) {
return;
}
mOnWindowShownListeners.remove(victim);
}
2.11、OnDrawListener监听器注册/移除
注册监听器:
/**
* <p>Register a callback to be invoked when the view tree is about to be drawn.</p>
* <p><strong>Note:</strong> this method <strong>cannot</strong> be invoked from
* {@link android.view.ViewTreeObserver.OnDrawListener#onDraw()}.</p>
*
* @param listener The callback to add
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*/
public void addOnDrawListener(OnDrawListener listener) {
checkIsAlive();
if (mOnDrawListeners == null) {
mOnDrawListeners = new ArrayList<OnDrawListener>();
}
if (mInDispatchOnDraw) {
IllegalStateException ex = new IllegalStateException(
"Cannot call addOnDrawListener inside of onDraw");
if (sIllegalOnDrawModificationIsFatal) {
throw ex;
} else {
Log.e("ViewTreeObserver", ex.getMessage(), ex);
}
}
mOnDrawListeners.add(listener);
}移除监听器:
/**
* <p>Remove a previously installed pre-draw callback.</p>
* <p><strong>Note:</strong> this method <strong>cannot</strong> be invoked from
* {@link android.view.ViewTreeObserver.OnDrawListener#onDraw()}.</p>
*
* @param victim The callback to remove
*
* @throws IllegalStateException If {@link #isAlive()} returns false
*
* @see #addOnDrawListener(OnDrawListener)
*/
public void removeOnDrawListener(OnDrawListener victim) {
checkIsAlive();
if (mOnDrawListeners == null) {
return;
}
if (mInDispatchOnDraw) {
IllegalStateException ex = new IllegalStateException(
"Cannot call removeOnDrawListener inside of onDraw");
if (sIllegalOnDrawModificationIsFatal) {
throw ex;
} else {
Log.e("ViewTreeObserver", ex.getMessage(), ex);
}
}
mOnDrawListeners.remove(victim);
}
3、ViewTreeObserver监听器原理
观察者模式其实就是在合适的地方通知观察者,找准合适的位置很重要。这十一种观察者,每个都对应一个分发事件的方法:
final void dispatchOnWindowAttachedChange(boolean attached) {
final void dispatchOnWindowFocusChange(boolean hasFocus) {
final void dispatchOnGlobalFocusChange(View oldFocus, View newFocus) { View-->ViewTreeObserver
public final void dispatchOnGlobalLayout() {
public final void dispatchOnWindowShown()
public final boolean dispatchOnPreDraw(){
public final void dispatchOnDraw() {
final void dispatchOnTouchModeChanged(boolean inTouchMode) {
final void dispatchOnScrollChanged() {
final void dispatchOnComputeInternalInsets(InternalInsetsInfo inoutInfo) {
public final void dispatchOnEnterAnimationComplete() { ActivityThread-->Activity-->ViewTreeObserver回调方法早已埋在系统框架中,当消息来了就调用这些方法通知注册的观察者。以下九个回调方法都在ViewRootImpl中回调:
dispatchOnWindowAttachedChange(boolean attached)
dispatchOnWindowFocusChange(boolean hasFocus)
dispatchOnGlobalLayout()
dispatchOnWindowShown()
dispatchOnPreDraw()
dispatchOnDraw()
dispatchOnTouchModeChanged(boolean inTouchMode)
dispatchOnScrollChanged()
dispatchOnComputeInternalInsets(InternalInsetsInfo inoutInfo)
3.1、ViewRootImpl中的回调
熟悉View绘制流程的开发者对ViewRootImpl一定不陌生,ViewRootImpl不仅是视图和WM沟通的桥梁,也是事件分发的桥梁。其中有一个重要的方法:doTraversal(),真正执行视图树遍历绘制的方法。
这里以ViewTreeObserver的dispatchOnWindowFocusChange()方法回调为例,它的回调流程如下:
而另外两个则不太一样,继续往下看源码:
3.2、View中回调dispatchOnGlobalFocusChange
回调链路:View-->ViewTreeObserver。
3.3、Activity中回调dispatchOnEnterAnimationComplete
回调链路:ActivityThread-->Activity-->ViewTreeObserver。
博客:
探究onAttachedToWindow和onDetachedFromWindow


