ViewTreeObserver for android

Source: Internet
Author: User

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);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.