Basic concepts of Class I
This is an observer (observer) that registers the monitoring view tree and 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.
Class II Main Interface: Listener
Interface ViewTreeObserver. OnGlobalFocusChangeListe Ner Interface Class of the callback function to be called when the focus status in a view tree changes
Interface ViewTreeObserver. OnGlobalLayoutListener interface class 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
Interface ViewTreeObserver. OnPreDrawListener interface class of the callback function to be called when a view tree is to be drawn
Interface ViewTreeObserver. OnScrollChangedListener interface class of the callback function to be called when some components in the view tree scroll
Interface ViewTreeObserver. OnTouchModeChangeListene R when the touch mode of a view tree changes, three sample codes of the interface class of the callback function to be called
1. Create a listener
Private final ViewTreeObserver. onGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver. onGlobalLayoutListener () {@ Override public void onGlobalLayout () {int width =-1; int height =-1; try {width = getActivity (). getWindow (). getDecorView (). getWidth (); height = getActivity (). getWindow (). getDecorView (). getHeight ();} catch (Exception e) {// called too early. so, just skip .} if (width ! =-1 & mGlobalLayoutWidth! = Width) {// only when the size has a value, that is, it is determined that the UI update makes sense for mGlobalLayoutWidth = width; updateUI ();} else if (height! =-1 & mGlobalLayoutHeight! = Height) {mGlobalLayoutHeight = height; updateUI ();}}};
2. Generally, the listener is registered in onCreate or onCreateView.
mViewTreeObserver = getActivity().getWindow().getDecorView().getViewTreeObserver(); mViewTreeObserver.addOnGlobalLayoutListener(mGlobalLayoutListener);