问题
I had posted this yeaterday.As you can see the problem was that I am trying to show a context menu and dismiss it using the same button.The previous problem was,when I was clicking the button the menu was showing,but then it was not closing.One of my friend here suggested to change: mPopupMenu.setModal(false);
which was true
previously.Now the menu is showing and dismissing perfectly on button click.But the new problem is onItemClick
of the menu is not working now.Tried a lot but couldnt find a solution.Here is the code:
mPopupMenu = new IcsListPopupWindow(this);
mAdapter = new PopupMenuAdapter(this, R.layout.popmenu_row/*android.R.layout.simple_list_item_1*/, poparray);
mPopupMenu.setAdapter(mAdapter);
mPopupMenu.setModal(false);
mPopupMenu.setOnItemClickListener(ContactsActivity.this);
// only if you need it
menuicon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(f==1){
f=0;
mPopupMenu.setModal(true);
mPopupMenu.setContentWidth(ContactsActivity.this.getWindowManager().getDefaultDisplay().getWidth() / 2);
mAdapter.notifyDataSetChanged(); // if you change anything
mPopupMenu.setAnchorView(menuicon);
mPopupMenu.show();
}
else{
// mPopupMenu.setModal(false);
mPopupMenu.dismiss();
f=1;
}
}
});
Here I am showing the button menuicon in actionbar.I am using actionbarsherlock library.
回答1:
You are trying to toggling the mPopupMenu view by using button click. if i am correct try the following code.
menuicon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mPopupMenu != null){
if(mPopupMenu.isShowing()){
mPopupMenu.dismiss();
}else{
mPopupMenu.setModal(true);
mPopupMenu.setContentWidth(ContactsActivity.this.getWindowManager().getDefaultDisplay().getWidth() / 2);
mAdapter.notifyDataSetChanged(); // if you change anything
mPopupMenu.setAnchorView(menuicon);
mPopupMenu.show();
}
}
}
});
来源:https://stackoverflow.com/questions/23645985/imageview-on-click-not-firing-at-the-2nd-click-part-2