Code example for ListPopupWindow

Methods: dismiss, isShowing, show

1
        return true; 
    } 
 
    public void dismiss() { 
        if (isShowing()) { 
            mPopup.dismiss();
        } 
    } 
 
    public void onDismiss() { 
        mPopup = null;
        mMenu.close();
        if (mTreeObserver != null) {
            if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
            mTreeObserver.removeGlobalOnLayoutListener(this);
            mTreeObserver = null;
        } 
        mAnchorView.removeOnAttachStateChangeListener(this);
    } 
 
    public boolean isShowing() { 
        return mPopup != null && mPopup.isShowing();
    } 
 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MenuAdapter adapter = mAdapter;
        adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
    } 
 
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
            dismiss(); 
            return true; 
        } 
        return false; 
    } 
 
    private int measureContentWidth(ListAdapter adapter) {
        // Menus don't tend to be long, so this is more sane than it looks. 
        int width = 0;
        View itemView = null;
        int itemType = 0;
        final int widthMeasureSpec =
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        final int heightMeasureSpec =
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        final int count = adapter.getCount();
        for (int i = 0; i < count; i++) {
            final int positionType = adapter.getItemViewType(i);
            if (positionType != itemType) {
                itemType = positionType;
                itemView = null;
            } 
            if (mMeasureParent == null) {
                mMeasureParent = new FrameLayout(mContext);
            } 
            itemView = adapter.getView(i, itemView, mMeasureParent);
            itemView.measure(widthMeasureSpec, heightMeasureSpec);
            width = Math.max(width, itemView.getMeasuredWidth());
        } 
        return width;
    } 
 
    @Override 
    public void onGlobalLayout() { 
        if (isShowing()) { 
            final View anchor = mAnchorView;
            if (anchor == null || !anchor.isShown()) {
                dismiss(); 
            } else if (isShowing()) { 
                // Recompute window size and position 
                mPopup.show();
            } 
        } 
    } 
 
    @Override