質問

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