Java Code Examples for android.view.ViewTreeObserver#isAlive()

The following examples show how to use android.view.ViewTreeObserver#isAlive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MaterialTapTargetPrompt.java    From MaterialTapTargetPrompt with Apache License 2.0 6 votes vote down vote up
/**
 * Removes global layout listener added in {@link #addGlobalLayoutListener()}.
 */
void removeGlobalLayoutListener()
{
    final ViewGroup parent = (ViewGroup) mView.getParent();
    if (parent == null)
    {
        return;
    }
    final ViewTreeObserver viewTreeObserver = ((ViewGroup) mView.getParent()).getViewTreeObserver();
    if (viewTreeObserver.isAlive())
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            viewTreeObserver.removeOnGlobalLayoutListener(mGlobalLayoutListener);
        }
        else
        {
            viewTreeObserver.removeGlobalOnLayoutListener(mGlobalLayoutListener);
        }
    }
}
 
Example 2
Source File: ViewTarget.java    From giffun with Apache License 2.0 6 votes vote down vote up
private void checkCurrentDimens() {
    if (cbs.isEmpty()) {
        return;
    }

    int currentWidth = getViewWidthOrParam();
    int currentHeight = getViewHeightOrParam();
    if (!isSizeValid(currentWidth) || !isSizeValid(currentHeight)) {
        return;
    }

    notifyCbs(currentWidth, currentHeight);
    // Keep a reference to the layout listener and remove it here
    // rather than having the observer remove itself because the observer
    // we add the listener to will be almost immediately merged into
    // another observer and will therefore never be alive. If we instead
    // keep a reference to the listener and remove it here, we get the
    // current view tree observer and should succeed.
    ViewTreeObserver observer = view.getViewTreeObserver();
    if (observer.isAlive()) {
        observer.removeOnPreDrawListener(layoutListener);
    }
    layoutListener = null;
}
 
Example 3
Source File: ActivityChooserView.java    From zen4android with MIT License 6 votes vote down vote up
@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    ActivityChooserModel dataModel = mAdapter.getDataModel();
    if (dataModel != null) {
        try {
            dataModel.unregisterObserver(mModelDataSetOberver);
        } catch (IllegalStateException e) {
            //Oh, well... fixes issue #557
        }
    }
    ViewTreeObserver viewTreeObserver = getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
    }
    mIsAttachedToWindow = false;
}
 
Example 4
Source File: Profile.java    From Slide with GNU General Public License v3.0 6 votes vote down vote up
private void scrollToTabAfterLayout(final int tabIndex) {
    //from http://stackoverflow.com/a/34780589/3697225
    if (tabs != null) {
        final ViewTreeObserver observer = tabs.getViewTreeObserver();

        if (observer.isAlive()) {
            observer.dispatchOnGlobalLayout(); // In case a previous call is waiting when this call is made
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    tabs.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    tabs.getTabAt(tabIndex).select();
                }
            });
        }
    }
}
 
Example 5
Source File: LithoScrollView.java    From litho with Apache License 2.0 6 votes vote down vote up
void mount(
    ComponentTree contentComponentTree,
    final ScrollPosition scrollPosition,
    boolean isIncrementalMountEnabled) {
  mLithoView.setComponentTree(contentComponentTree);

  mIsIncrementalMountEnabled = isIncrementalMountEnabled;
  mScrollPosition = scrollPosition;
  final ViewTreeObserver.OnPreDrawListener onPreDrawListener =
      new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
          setScrollY(scrollPosition.y);
          ViewTreeObserver currentViewTreeObserver = getViewTreeObserver();
          if (currentViewTreeObserver.isAlive()) {
            currentViewTreeObserver.removeOnPreDrawListener(this);
          }
          return true;
        }
      };
  getViewTreeObserver().addOnPreDrawListener(onPreDrawListener);

  mOnPreDrawListener = onPreDrawListener;
}
 
Example 6
Source File: NewsActivity.java    From Slide with GNU General Public License v3.0 6 votes vote down vote up
private void scrollToTabAfterLayout(final int tabIndex) {
    //from http://stackoverflow.com/a/34780589/3697225
    if (mTabLayout != null) {
        final ViewTreeObserver observer = mTabLayout.getViewTreeObserver();

        if (observer.isAlive()) {
            observer.dispatchOnGlobalLayout(); // In case a previous call is waiting when this call is made
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mTabLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    mTabLayout.getTabAt(tabIndex).select();
                }
            });
        }
    }
}
 
Example 7
Source File: FloatingActionsMenu.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
    if (mVisible != visible || force) {
        mVisible = visible;
        int height = getHeight();
        if (height == 0 && !force) {
            ViewTreeObserver vto = getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggle(visible, animate, true);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height + getMarginBottom();
        if (animate) {
            animate().setInterpolator(mInterpolator)
                .setDuration(TRANSLATE_DURATION_MILLIS)
                .translationY(translationY);
        } else {
            setTranslationY(translationY);
        }
        // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
        if (!Utils.hasHoneycomb()) {
            setClickable(visible);
        }
    }

    if(isMenuOpen()) {
        closeMenu();
    }
}
 
Example 8
Source File: FilePicker.java    From faceswap with Apache License 2.0 5 votes vote down vote up
/**
 * Toggles the material floating action button.
 *
 * @param visible
 */
public void toggleButton(final boolean visible) {
    if (isFabShowing != visible) {
        isFabShowing = visible;
        int height = fab.getHeight();
        if (height == 0) {
            ViewTreeObserver vto = fab.getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = fab.getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggleButton(visible);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height;
        fab.animate().setInterpolator(interpolator)
                .setDuration(350)
                .translationY(translationY);

        // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
        fab.setClickable(visible);
    }
}
 
Example 9
Source File: PhotoViewAttacher.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 10
Source File: FloatingActionButton.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
    if (mVisible != visible || force) {
        mVisible = visible;
        int height = getHeight();
        if (height == 0 && !force) {
            ViewTreeObserver vto = getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggle(visible, animate, true);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height + getMarginBottom();
        if (animate) {
            animate().setInterpolator(mInterpolator)
                .setDuration(TRANSLATE_DURATION_MILLIS)
                .translationY(translationY);
        } else {
            setTranslationY(translationY);
        }
        // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
        if (!Utils.hasHoneycomb()) {
            setClickable(visible);
        }
    }

}
 
Example 11
Source File: FilePicker.java    From FilePickerLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * Toggles the material floating action button.
 *
 * @param visible
 */
public void toggleButton(final boolean visible) {
    if (isFabShowing != visible) {
        isFabShowing = visible;
        int height = fab.getHeight();
        if (height == 0) {
            ViewTreeObserver vto = fab.getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = fab.getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggleButton(visible);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height;
        fab.animate().setInterpolator(interpolator)
                .setDuration(350)
                .translationY(translationY);

        // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
        fab.setClickable(visible);
    }
}
 
Example 12
Source File: PhotoViewAttacher.java    From Dashboard with MIT License 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called
 * when the ImageView is no longer used. A good example is from
 * {@link android.view.View#onDetachedFromWindow()} or from
 * {@link android.app.Activity#onDestroy()}. This is automatically called if
 * you are using {@link com.antonioleiva.materialeverywhere.uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public final void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 13
Source File: PhotoViewAttacher.java    From Tweetin with Apache License 2.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link android.view.View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 14
Source File: FloatingActionsMenu.java    From TestChat with Apache License 2.0 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
        if (mVisible != visible || force) {
                mVisible = visible;
                int height = getHeight();
                if (height == 0 && !force) {
                        ViewTreeObserver vto = getViewTreeObserver();
                        if (vto.isAlive()) {
                                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                                        @Override
                                        public boolean onPreDraw() {
                                                ViewTreeObserver currentVto = getViewTreeObserver();
                                                if (currentVto.isAlive()) {
                                                        currentVto.removeOnPreDrawListener(this);
                                                }
                                                toggle(visible, animate, true);
                                                return true;
                                        }
                                });
                                return;
                        }
                }
                int translationY = visible ? 0 : height + mAddButton.getMarginBottom();
                if (animate) {
                        ViewPropertyAnimator.animate(this).setInterpolator(mInterpolator)
                                .setDuration(TRANSLATE_DURATION_MILLIS)
                                .translationY(translationY);
                } else {
                        ViewHelper.setTranslationY(this, translationY);
                }

                // On pre-Honeycomb a translated view is still clickable, so we need to disable clicks manually
                if (!hasHoneycombApi()) {
                        setClickable(visible);
                }
        }
}
 
Example 15
Source File: FloatActionButtonScrollDetectorHelper.java    From talk-android with MIT License 5 votes vote down vote up
private void toggle(final boolean visible, final boolean animate, boolean force) {
    if (mVisible != visible || force) {
        mVisible = visible;
        int height = floatActionButton.getHeight();
        if (height == 0 && !force) {
            ViewTreeObserver vto = floatActionButton.getViewTreeObserver();
            if (vto.isAlive()) {
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        ViewTreeObserver currentVto = floatActionButton.getViewTreeObserver();
                        if (currentVto.isAlive()) {
                            currentVto.removeOnPreDrawListener(this);
                        }
                        toggle(visible, animate, true);
                        return true;
                    }
                });
                return;
            }
        }
        int translationY = visible ? 0 : height + getMarginBottom();
        if (animate) {
            ViewPropertyAnimator.animate(floatActionButton).setInterpolator(mInterpolator)
                    .setDuration(TRANSLATE_DURATION_MILLIS)
                    .translationY(translationY);
        } else {
            ViewHelper.setTranslationY(floatActionButton, translationY);
        }
    }
}
 
Example 16
Source File: PhotoViewAttacher.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link android.view.View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 17
Source File: MaterialTapTargetPrompt.java    From MaterialTapTargetPrompt with Apache License 2.0 5 votes vote down vote up
/**
 * Adds layout listener to view parent to capture layout changes.
 */
void addGlobalLayoutListener()
{
    final ViewTreeObserver viewTreeObserver = ((ViewGroup) mView.getParent()).getViewTreeObserver();
    if (viewTreeObserver.isAlive())
    {
        viewTreeObserver.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }
}
 
Example 18
Source File: ViewUtils.java    From Scrollable with Apache License 2.0 5 votes vote down vote up
static void removeGlobalLayoutListener(View view, ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener) {

        final ViewTreeObserver observer = view.getViewTreeObserver();
        if (!observer.isAlive()) {
            return;
        }

        if (Build.VERSION.SDK_INT >= 16) {
            observer.removeOnGlobalLayoutListener(onGlobalLayoutListener);
        } else {
            //noinspection deprecation
            observer.removeGlobalOnLayoutListener(onGlobalLayoutListener);
        }
    }
 
Example 19
Source File: PhotoViewAttacher.java    From star-zone-android with Apache License 2.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link android.view.View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 20
Source File: AnimatedLinearLayout.java    From MVPAndroidBootstrap with Apache License 2.0 4 votes vote down vote up
private void waitForNextFrame() {
    ViewTreeObserver treeObserver = getViewTreeObserver();
    if (treeObserver != null && treeObserver.isAlive()) {
        treeObserver.addOnPreDrawListener(new PreDrawListener());
    }
}