Example usage for android.view View addOnAttachStateChangeListener

List of usage examples for android.view View addOnAttachStateChangeListener

Introduction

In this page you can find the example usage for android.view View addOnAttachStateChangeListener.

Prototype

public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) 

Source Link

Document

Add a listener for attach state changes.

Usage

From source file:com.desmond.ripple.view.RippleCompat.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void handleAttach(final View v, final RippleCompatDrawable drawable) {
    if (Build.VERSION.SDK_INT >= 12) {
        v.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override/*from   www. jav  a2 s.c o  m*/
            public void onViewAttachedToWindow(View v) {
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                drawable.finishRipple();
            }
        });
    }
}

From source file:Main.java

public static void scaleShow(final View view, final Runnable rWhenEnd) {
    if (view.getVisibility() == View.VISIBLE) {
        view.setVisibility(View.VISIBLE);
        if (rWhenEnd != null) {
            rWhenEnd.run();/*from   w  w w . j  a  va  2  s  .c  om*/
        }
        return;
    }
    if (view.getWindowToken() == null) {
        view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                scaleShow(view);
            }

            @Override
            public void onViewDetachedFromWindow(View v) {

            }
        });
    }
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f);
    AnimatorSet set = new AnimatorSet();
    set.playTogether(scaleX, scaleY);
    set.setDuration(DURATION_SHORT);
    set.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view.setVisibility(View.VISIBLE);
            if (rWhenEnd != null) {
                rWhenEnd.run();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            view.setVisibility(View.VISIBLE);
            if (rWhenEnd != null) {
                rWhenEnd.run();
            }
        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    set.start();
}

From source file:nuclei.ui.LoadingManager.java

void showLoading(final Integer id, final View view, final boolean immediate) {
    if (mActivity != null)
        mActivity.runOnUiThread(new Runnable() {
            @Override//  w  w  w.  j a va 2  s  . c  o m
            public void run() {
                if (mLoadingTokens.contains(id)) {
                    if (mLoadingView == null && ViewCompat.isAttachedToWindow(view)) {
                        mLoadingView = LoadingView.make(mActivity, view, immediate);
                    } else if (mLoadingView == null) {
                        view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                            @Override
                            public void onViewAttachedToWindow(View v) {
                                showLoading(id, view, immediate);
                                view.removeOnAttachStateChangeListener(this);
                            }

                            @Override
                            public void onViewDetachedFromWindow(View v) {
                                view.removeOnAttachStateChangeListener(this);
                            }
                        });
                    }
                }
            }
        });
}

From source file:com.nile.kmooc.view.custom.popup.menu.MenuPopupHelper.java

public boolean tryShow() {
    mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
    mPopup.setOnDismissListener(this);
    mPopup.setOnItemClickListener(this);
    mPopup.setAdapter(mAdapter);/*from   w w w.  ja v a2  s. c om*/
    mPopup.setModal(true);

    View anchor = mAnchorView;
    if (anchor != null) {
        final boolean addGlobalListener = mTreeObserver == null;
        mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
        if (addGlobalListener)
            mTreeObserver.addOnGlobalLayoutListener(this);
        anchor.addOnAttachStateChangeListener(this);
        mPopup.setAnchorView(anchor);
        mPopup.setDropDownGravity(mDropDownGravity);
    } else {
        return false;
    }

    if (mContentWidth == Integer.MIN_VALUE) {
        mContentWidth = measureContentWidth();
    }
    mPopup.setContentWidth(mContentWidth);
    // Invert the horizontal offset in RTL mode.
    if (ViewCompat.getLayoutDirection(mAnchorView) == ViewCompat.LAYOUT_DIRECTION_RTL) {
        mPopup.setHorizontalOffset(-mPopup.getHorizontalOffset());
    }
    // Implement right gravity manually through horizontal offset pre-KitKat.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT
            && (Gravity.getAbsoluteGravity(mDropDownGravity, ViewCompat.getLayoutDirection(anchor))
                    & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.RIGHT) {
        mPopup.setHorizontalOffset(mPopup.getHorizontalOffset() + (mAnchorView.getWidth() - mPopup.getWidth()));
    }
    // If vertical offset is defined as 0, then ListPopupWindow infers
    // it as the negative of the top padding of the background, in
    // order to anchor the content area. Since that is not the effect
    // we want, we'll force it to use only the explicitly defined
    // offset by explicitly setting it dynamically as well, and thus
    // forcing it to discard it's 'unset' flag.
    mPopup.setVerticalOffset(mPopup.getVerticalOffset());
    // Top/bottom padding will be applied on the background drawable,
    // as the ListView is both initialized and set up only after show()
    // is called on the ListPopupWindow. Left/right padding will be
    // set up on the list items from the adapter, to keep the correct
    // item boundaries for the selector.
    ShapeDrawable paddedDrawable = new ShapeDrawable();
    paddedDrawable.setAlpha(0);
    // Don't apply top padding if the first item is a header, to
    // comply with the design.
    paddedDrawable.setPadding(0, mAdapter.hasHeader() ? 0 : (mPopupPaddingTop - mPopupItemVerticalPadding), 0,
            mPopupPaddingBottom - mPopupItemVerticalPadding);
    Drawable background = mPopup.getBackground();
    mPopup.setBackgroundDrawable(background == null ? paddedDrawable
            : new LayerDrawable(new Drawable[] { background, paddedDrawable }));
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
    mPopup.show();
    mPopup.getListView().setOnKeyListener(this);
    return true;
}

From source file:com.facebook.react.ReactInstanceManager.java

/**
 * Use this method when the activity resumes.
 *///from   w w w . j  av  a  2s  .  co m
@ThreadConfined(UI)
public void onHostResume(Activity activity) {
    UiThreadUtil.assertOnUiThread();

    mCurrentActivity = activity;

    if (mUseDeveloperSupport) {
        // Resume can be called from one of two different states:
        // a) when activity was paused
        // b) when activity has just been created
        // In case of (a) the activity is attached to window and it is ok to add new views to it or
        // open dialogs. In case of (b) there is often a slight delay before such a thing happens.
        // As dev support manager can add views or open dialogs immediately after it gets enabled
        // (e.g. in the case when JS bundle is being fetched in background) we only want to enable
        // it once we know for sure the current activity is attached.

        // We check if activity is attached to window by checking if decor view is attached
        final View decorView = mCurrentActivity.getWindow().getDecorView();
        if (!ViewCompat.isAttachedToWindow(decorView)) {
            decorView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                @Override
                public void onViewAttachedToWindow(View v) {
                    // we can drop listener now that we know the view is attached
                    decorView.removeOnAttachStateChangeListener(this);
                    mDevSupportManager.setDevSupportEnabled(true);
                }

                @Override
                public void onViewDetachedFromWindow(View v) {
                    // do nothing
                }
            });
        } else {
            // activity is attached to window, we can enable dev support immediately
            mDevSupportManager.setDevSupportEnabled(true);
        }
    }

    moveToResumedLifecycleState(false);
}