문제

I want to close a popup menu on clicking a imageview.But it is not working.

menuicon.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        if (f == 1) {
            f = 0;
            mPopupMenu.setContentWidth(ContactsActivity.this.getWindowManager()
                .getDefaultDisplay()
                .getWidth() / 2);
            mAdapter.notifyDataSetChanged();
            mPopupMenu.setAnchorView(menuicon);
            mPopupMenu.show();

        } else {
            mPopupMenu.dismiss();
            f = 1;

        }

    }
});

When I click the menuicon first time the popup menu is displaying correctly.But when i click for the 2nd time the menu is not closing.I debugged my code and found that the imageview is actually not firing roe 2nd time.menuicon is an imageview.And here is xml code:

<ImageView
            android:id="@+id/menuicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/menuicon"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp" 

        />       

Initial value of f is 1.

도움이 되었습니까?

해결책

You need to call setModal(false) of mPopupMenu. It will call setFocusable of inner PopupWindow and allow you to get events in the background window.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top