Structure
public final class
ViewTreeObserver
extends Object
java.lang.Object
↳ android.view.ViewTreeObserver
继承Object
Class Overview
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 getViewTreeObserver() for more information.
此为监听本视图树是否有变化的观察者,这些变化包括整个树的布局,开始绘图,触摸方式发生改变….等等,此观察者不能实例化。
Summery
Interface
ViewTreeObserver.OnDrawListener 监听视图树将被绘图 ViewTreeObserver.OnPreDrawListener 同上 ViewTreeObserver.OnGlobalFocusChangeListener 监听焦点 ViewTreeObserver.OnGlobalLayoutListener 状态、可见性 ViewTreeObserver.OnScrollChangedListener 滚动 ViewTreeObserver.OnTouchModeChangeListener 触摸模式 ViewTreeObserver.OnWindowAttachListener 从win添加or者分离视图结构
Method
public void
addOnDrawListener(ViewTreeObserver.OnDrawListener listener)
removeOnDrawListener(ViewTreeObserver.OnDrawListener victim)
…………
查看具体API文档Example: view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //do something //视图会一直重绘,故要及时撤销监听 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { view.getViewTreeObserver().removeGlobalOnLayoutListener(this); } } });