Example usage for android.view ViewTreeObserver addOnGlobalLayoutListener

List of usage examples for android.view ViewTreeObserver addOnGlobalLayoutListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver addOnGlobalLayoutListener.

Prototype

public void addOnGlobalLayoutListener(OnGlobalLayoutListener listener) 

Source Link

Document

Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes

Usage

From source file:android.support.v7.app.MediaRouteControllerDialog.java

private void startGroupListFadeInAnimation() {
    clearGroupListAnimation(true);//from   w  w  w.j  av a 2  s  .co m
    mVolumeGroupList.requestLayout();
    ViewTreeObserver observer = mVolumeGroupList.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mVolumeGroupList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            startGroupListFadeInAnimationInternal();
        }
    });
}

From source file:android.support.v7.app.MediaRouteControllerDialog.java

private void animateGroupListItems(final Map<MediaRouter.RouteInfo, Rect> previousRouteBoundMap,
        final Map<MediaRouter.RouteInfo, BitmapDrawable> previousRouteBitmapMap) {
    mVolumeGroupList.setEnabled(false);/*from   w w  w . ja  va  2 s .  co  m*/
    mVolumeGroupList.requestLayout();
    mIsGroupListAnimating = true;
    ViewTreeObserver observer = mVolumeGroupList.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mVolumeGroupList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            animateGroupListItemsInternal(previousRouteBoundMap, previousRouteBitmapMap);
        }
    });
}

From source file:android.support.v7.app.MediaRouteControllerDialog.java

private void updateLayoutHeight(final boolean animate) {
    // We need to defer the update until the first layout has occurred, as we don't yet know the
    // overall visible display size in which the window this view is attached to has been
    // positioned in.
    mDefaultControlLayout.requestLayout();
    ViewTreeObserver observer = mDefaultControlLayout.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override//from   ww  w . ja v  a 2 s  . c om
        public void onGlobalLayout() {
            mDefaultControlLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            if (mIsGroupListAnimating) {
                mIsGroupListAnimationPending = true;
            } else {
                updateLayoutHeightInternal(animate);
            }
        }
    });
}

From source file:com.google.samples.apps.iosched.ui.CurrentSessionActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    UIUtils.tryTranslateHttpIntent(this);
    BeamUtils.tryUpdateIntentFromBeam(this);
    boolean shouldBeFloatingWindow = false;
    if (shouldBeFloatingWindow) {
        setupFloatingWindow();/*from www.jav a2s.  c o  m*/
    }
    super.onCreate(savedInstanceState);
    mSessionId = getCurrentSessionId();
    if (mSessionId == null) {
        complainMissingCurrentTalkId();
    } else {
        setContentView(R.layout.activity_current_session);

        final Toolbar toolbar = getActionBarToolbar();
        toolbar.setNavigationIcon(shouldBeFloatingWindow ? R.drawable.ic_ab_close : R.drawable.ic_up);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                toolbar.setTitle("");
            }
        });

        mSessionUri = ScheduleContract.Sessions.buildSessionUri(mSessionId);

        mFABElevation = getResources().getDimensionPixelSize(R.dimen.fab_elevation);
        mMaxHeaderElevation = getResources().getDimensionPixelSize(R.dimen.session_detail_max_header_elevation);

        mTagColorDotSize = getResources().getDimensionPixelSize(R.dimen.tag_color_dot_size);

        mHandler = new Handler();

        if (mSpeakersImageLoader == null) {
            mSpeakersImageLoader = new ImageLoader(this, R.drawable.person_image_empty);
        }
        if (mNoPlaceholderImageLoader == null) {
            mNoPlaceholderImageLoader = new ImageLoader(this);
        }

        mScrollView = (ObservableScrollView) findViewById(R.id.scroll_view);
        mScrollView.addCallbacks(this);
        ViewTreeObserver vto = mScrollView.getViewTreeObserver();
        if (vto.isAlive()) {
            vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
        }

        mScrollViewChild = findViewById(R.id.scroll_view_child);
        mScrollViewChild.setVisibility(View.INVISIBLE);

        mDetailsContainer = findViewById(R.id.details_container);
        mHeaderBox = findViewById(R.id.header_session);
        mTitle = (TextView) findViewById(R.id.session_title);
        mSubtitle = (TextView) findViewById(R.id.session_subtitle);
        mPhotoViewContainer = findViewById(R.id.session_photo_container);
        mPhotoView = (ImageView) findViewById(R.id.session_photo);

        mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
        mAbstract = (TextView) findViewById(R.id.session_abstract);
        mRequirements = (TextView) findViewById(R.id.session_requirements);
        mTags = (LinearLayout) findViewById(R.id.session_tags);
        mTagsContainer = (ViewGroup) findViewById(R.id.session_tags_container);

        mAddScheduleButton = (CheckableFrameLayout) findViewById(R.id.add_schedule_button);
        mAddScheduleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean starred = !mStarred;
                SessionsHelper helper = new SessionsHelper(CurrentSessionActivity.this);
                showStarred(starred, true);
                helper.setSessionStarred(mSessionUri, starred, mTitleString);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mAddScheduleButton.announceForAccessibility(
                            starred ? getString(R.string.session_details_a11y_session_added)
                                    : getString(R.string.session_details_a11y_session_removed));
                }

                /* [ANALYTICS:EVENT]
                 * TRIGGER:   Add or remove a session from My Schedule.
                 * CATEGORY:  'Session'
                 * ACTION:    'Starred' or 'Unstarred'
                 * LABEL:     Session title/subtitle.
                 * [/ANALYTICS]
                 */
                AnalyticsManager.sendEvent("Session", starred ? "Starred" : "Unstarred", mTitleString, 0L);
            }
        });

        ViewCompat.setTransitionName(mPhotoView, TRANSITION_NAME_PHOTO);

        LoaderManager manager = getLoaderManager();
        manager.initLoader(SessionsQuery._TOKEN, null, this);
        manager.initLoader(SpeakersQuery._TOKEN, null, this);
        manager.initLoader(TAG_METADATA_TOKEN, null, this);
    }
}

From source file:com.saarang.samples.apps.iosched.ui.SessionDetailActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    UIUtils.tryTranslateHttpIntent(this);
    BeamUtils.tryUpdateIntentFromBeam(this);
    boolean shouldBeFloatingWindow = shouldBeFloatingWindow();
    if (shouldBeFloatingWindow) {
        setupFloatingWindow();//from w w  w  .  ja v  a2 s . co m
    }

    super.onCreate(savedInstanceState);
    setContentView(com.saarang.samples.apps.iosched.R.layout.activity_session_detail);

    final Toolbar toolbar = getActionBarToolbar();
    toolbar.setNavigationIcon(shouldBeFloatingWindow ? com.saarang.samples.apps.iosched.R.drawable.ic_ab_close
            : com.saarang.samples.apps.iosched.R.drawable.ic_up);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            toolbar.setTitle("");
        }
    });

    if (savedInstanceState == null) {
        Uri sessionUri = getIntent().getData();
        BeamUtils.setBeamSessionUri(this, sessionUri);
    }

    mSessionUri = getIntent().getData();

    if (mSessionUri == null) {
        return;
    }

    mSessionId = ScheduleContract.Sessions.getSessionId(mSessionUri);

    mFABElevation = getResources()
            .getDimensionPixelSize(com.saarang.samples.apps.iosched.R.dimen.fab_elevation);
    mMaxHeaderElevation = getResources().getDimensionPixelSize(
            com.saarang.samples.apps.iosched.R.dimen.session_detail_max_header_elevation);

    mTagColorDotSize = getResources()
            .getDimensionPixelSize(com.saarang.samples.apps.iosched.R.dimen.tag_color_dot_size);

    mHandler = new Handler();

    if (mSpeakersImageLoader == null) {
        mSpeakersImageLoader = new ImageLoader(this,
                com.saarang.samples.apps.iosched.R.drawable.person_image_empty);
    }
    if (mNoPlaceholderImageLoader == null) {
        mNoPlaceholderImageLoader = new ImageLoader(this);
    }

    mScrollView = (ObservableScrollView) findViewById(com.saarang.samples.apps.iosched.R.id.scroll_view);
    mScrollView.addCallbacks(this);
    ViewTreeObserver vto = mScrollView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }

    mScrollViewChild = findViewById(com.saarang.samples.apps.iosched.R.id.scroll_view_child);
    mScrollViewChild.setVisibility(View.INVISIBLE);

    mDetailsContainer = findViewById(com.saarang.samples.apps.iosched.R.id.details_container);
    mHeaderBox = findViewById(com.saarang.samples.apps.iosched.R.id.header_session);
    mTitle = (TextView) findViewById(com.saarang.samples.apps.iosched.R.id.session_title);
    mSubtitle = (TextView) findViewById(com.saarang.samples.apps.iosched.R.id.session_subtitle);
    mPhotoViewContainer = findViewById(com.saarang.samples.apps.iosched.R.id.session_photo_container);
    mPhotoView = (ImageView) findViewById(com.saarang.samples.apps.iosched.R.id.session_photo);

    mPlusOneButton = (PlusOneButton) findViewById(com.saarang.samples.apps.iosched.R.id.plus_one_button);
    mAbstract = (TextView) findViewById(com.saarang.samples.apps.iosched.R.id.session_abstract);
    mRequirements = (TextView) findViewById(com.saarang.samples.apps.iosched.R.id.session_requirements);
    mTags = (LinearLayout) findViewById(com.saarang.samples.apps.iosched.R.id.session_tags);
    mTagsContainer = (ViewGroup) findViewById(com.saarang.samples.apps.iosched.R.id.session_tags_container);

    mAddScheduleButton = (CheckableFrameLayout) findViewById(
            com.saarang.samples.apps.iosched.R.id.add_schedule_button);
    mAddScheduleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean starred = !mStarred;
            SessionsHelper helper = new SessionsHelper(SessionDetailActivity.this);
            showStarred(starred, true);
            helper.setSessionStarred(mSessionUri, starred, mTitleString);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mAddScheduleButton.announceForAccessibility(starred
                        ? getString(
                                com.saarang.samples.apps.iosched.R.string.session_details_a11y_session_added)
                        : getString(
                                com.saarang.samples.apps.iosched.R.string.session_details_a11y_session_removed));
            }

            /* [ANALYTICS:EVENT]
             * TRIGGER:   Add or remove a session from My Schedule.
             * CATEGORY:  'Session'
             * ACTION:    'Starred' or 'Unstarred'
             * LABEL:     Session title/subtitle.
             * [/ANALYTICS]
             */
            AnalyticsManager.sendEvent("Session", starred ? "Starred" : "Unstarred", mTitleString, 0L);
        }
    });

    ViewCompat.setTransitionName(mPhotoView, TRANSITION_NAME_PHOTO);

    LoaderManager manager = getLoaderManager();
    manager.initLoader(SessionsQuery._TOKEN, null, this);
    manager.initLoader(SpeakersQuery._TOKEN, null, this);
    manager.initLoader(TAG_METADATA_TOKEN, null, this);
}

From source file:com.androzic.MapFragment.java

private void updateMapViewArea() {
    final ViewTreeObserver vto = map.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @SuppressLint("NewApi")
        @SuppressWarnings("deprecation")
        public void onGlobalLayout() {
            View root = getView();
            Rect area = new Rect();
            map.getLocalVisibleRect(area);
            View v = root.findViewById(R.id.topbar);
            if (v != null)
                area.top = v.getBottom();
            v = root.findViewById(R.id.bottombar);
            if (v != null)
                area.bottom = v.getTop();
            v = root.findViewById(R.id.rightbar);
            if (v != null)
                area.right = v.getLeft();
            if (mapButtons.isShown()) {
                // Landscape mode
                if (v != null)
                    area.bottom = mapButtons.getTop();
                else
                    area.right = mapButtons.getLeft();
            }/*  w  w  w .jav a2 s .c o m*/
            if (!area.isEmpty())
                map.updateViewArea(area);
            ViewTreeObserver ob;
            if (vto.isAlive())
                ob = vto;
            else
                ob = map.getViewTreeObserver();

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                ob.removeGlobalOnLayoutListener(this);
            } else {
                ob.removeOnGlobalLayoutListener(this);
            }
        }
    });
}

From source file:androidx.mediarouter.app.MediaRouteControllerDialog.java

void startGroupListFadeInAnimation() {
    clearGroupListAnimation(true);//from   w w  w .  j  a va  2  s  . c o  m
    mVolumeGroupList.requestLayout();
    ViewTreeObserver observer = mVolumeGroupList.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mVolumeGroupList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            startGroupListFadeInAnimationInternal();
        }
    });
}

From source file:androidx.mediarouter.app.MediaRouteControllerDialog.java

void updateLayoutHeight(final boolean animate) {
    // We need to defer the update until the first layout has occurred, as we don't yet know the
    // overall visible display size in which the window this view is attached to has been
    // positioned in.
    mDefaultControlLayout.requestLayout();
    ViewTreeObserver observer = mDefaultControlLayout.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override//from ww  w .j av a2s .c  o m
        public void onGlobalLayout() {
            mDefaultControlLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            if (mIsGroupListAnimating) {
                mIsGroupListAnimationPending = true;
            } else {
                updateLayoutHeightInternal(animate);
            }
        }
    });
}

From source file:de.dreier.mytargets.views.MaterialTapTargetPrompt.java

/**
 * Adds layout listener to view parent to capture layout changes.
 *//*from www.  ja  va  2s. co  m*/
private void addGlobalLayoutListener() {
    final ViewTreeObserver viewTreeObserver = getParentView().getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }
}

From source file:com.android.incallui.CallCardFragment.java

@Override
public void animateForNewOutgoingCall() {
    final ViewGroup parent = (ViewGroup) mPrimaryCallCardContainer.getParent();

    final ViewTreeObserver observer = getView().getViewTreeObserver();

    mIsAnimating = true;//from  w w w  .  j a  va 2  s.co  m

    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final ViewTreeObserver observer = getView().getViewTreeObserver();
            if (!observer.isAlive()) {
                return;
            }
            observer.removeOnGlobalLayoutListener(this);

            final LayoutIgnoringListener listener = new LayoutIgnoringListener();
            mPrimaryCallCardContainer.addOnLayoutChangeListener(listener);

            // Prepare the state of views before the slide animation
            final int originalHeight = mPrimaryCallCardContainer.getHeight();
            mPrimaryCallCardContainer.setTag(R.id.view_tag_callcard_actual_height, originalHeight);
            mPrimaryCallCardContainer.setBottom(parent.getHeight());

            // Set up FAB.
            mFloatingActionButtonContainer.setVisibility(View.GONE);
            mFloatingActionButtonController.setScreenWidth(parent.getWidth());

            mCallButtonsContainer.setAlpha(0);
            mCallStateLabel.setAlpha(0);
            mPrimaryName.setAlpha(0);
            mCallTypeLabel.setAlpha(0);
            mCallNumberAndLabel.setAlpha(0);

            assignTranslateAnimation(mCallStateLabel, 1);
            assignTranslateAnimation(mCallStateIcon, 1);
            assignTranslateAnimation(mPrimaryName, 2);
            assignTranslateAnimation(mCallNumberAndLabel, 3);
            assignTranslateAnimation(mCallTypeLabel, 4);
            assignTranslateAnimation(mCallButtonsContainer, 5);

            final Animator animator = getShrinkAnimator(parent.getHeight(), originalHeight);

            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mPrimaryCallCardContainer.setTag(R.id.view_tag_callcard_actual_height, null);
                    setViewStatePostAnimation(listener);
                    mIsAnimating = false;
                    InCallPresenter.getInstance().onShrinkAnimationComplete();
                }
            });
            animator.start();
        }
    });
}