ViewTreeObserver for Android
Two days ago, I wrote a new function to load more small functions. I used the ViewTreeObserver class of Android. Here I will explain this class in detail.
The basic concept is to register an observer (observer) that listens to the view tree. It is notified when the global event of the view tree changes. This global event not only includes the layout of the entire tree, but also changes in the touch mode from the painting process. The most common purpose is when the listener determines the value of the width and height of the view, which can be obtained to change the UI. Main interface: Listener interface ViewTreeObserver. OnGlobalFocusChangeListe Ner When the focus status in a view tree changes, the interface class of the callback function to be called ViewTreeObserver. OnGlobalLayoutListener interface of the callback function to be called when the global layout of a view tree is changed or the visual status of a view in the view tree is changed ViewTreeObserver. OnPreDrawListener interface of the callback function to be called when a view tree is to be drawn ViewTreeObserver. OnScrollChangedListener interface of the callback function to be called when some components in the view tree scroll ViewTreeObserver. OnTouchModeChangeListene R when the touch mode of a view tree changes, the interface code of the callback function to be called is as follows: Note: how to obtain the length and height of a control, I believe many of my friends will think this problem very simple at first glance. I can call getWidth and getMeasuredWidth in onCreate directly. But in fact, it is not simple. If you don't believe it, you can try it. In onCreate, you cannot obtain the length and width, which is always 0.