From f22801e9d6705ab2582a4131294111211d6afe46 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 18 Sep 2012 03:07:43 +0200 Subject: [PATCH] Changed the way we handle menu item clicks using MenuPopupHelper Overriding onItemClick() doesn't allow us to get the MenuItem that was clicked when at least one menu item is hidden. This is because the position in the list of visible menu items is returned. But that value can't be used to retrieve the corresponding MenuItem (getItem() uses the position as index into the list of all menu items). --- src/com/fsck/k9/activity/MessageList.java | 9 ++++---- src/com/fsck/k9/helper/MenuPopup.java | 27 ++++++++--------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index 3a72cceea..bf4415e9e 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -1985,10 +1985,9 @@ public class MessageList extends K9ListActivity implements OnItemClickListener { final MenuBuilder menu = new MenuBuilder(MessageList.this); getSupportMenuInflater().inflate(R.menu.message_list_item_context, menu); MenuPopup popup = new MenuPopup(MessageList.this, menu, v); - popup.setOnMenuItemClickListener( new MenuPopup.OnMenuItemClickListener() { + popup.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override - public void onMenuItemClick(int itemId){ - MenuItem item = menu.getItem(itemId); + public boolean onMenuItemClick(MenuItem item) { final List selection = getSelectionFromMessage(message); switch (item.getItemId()) { case R.id.reply: { @@ -2045,7 +2044,9 @@ public class MessageList extends K9ListActivity implements OnItemClickListener { } } - } } ); + return true; + } + }); popup.show(); } }; diff --git a/src/com/fsck/k9/helper/MenuPopup.java b/src/com/fsck/k9/helper/MenuPopup.java index d46dd54b9..d0b7d317c 100644 --- a/src/com/fsck/k9/helper/MenuPopup.java +++ b/src/com/fsck/k9/helper/MenuPopup.java @@ -1,33 +1,24 @@ - package com.fsck.k9.helper; import android.content.Context; import android.view.View; -import android.widget.AdapterView; import com.actionbarsherlock.internal.view.menu.MenuBuilder; import com.actionbarsherlock.internal.view.menu.MenuPopupHelper; +import com.actionbarsherlock.view.MenuItem; + public class MenuPopup extends MenuPopupHelper { - OnMenuItemClickListener onMenuItemClickListener; + private MenuBuilder mMenu; + public MenuPopup(Context context, MenuBuilder menu, View anchorView) { super(context, menu, anchorView); + mMenu = menu; } - public void setOnMenuItemClickListener( - OnMenuItemClickListener onMenuItemClickListener) { - this.onMenuItemClickListener = onMenuItemClickListener; - } - - @Override - public void onItemClick(AdapterView parent, View view, int position, - long id) { - super.onItemClick(parent, view, position, id); - if (onMenuItemClickListener != null) - onMenuItemClickListener.onMenuItemClick(position); - } - - public interface OnMenuItemClickListener{ - public void onMenuItemClick(int itemID); + public void setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener listener) { + for (int i = 0, end = mMenu.size(); i < end; i++) { + mMenu.getItem(i).setOnMenuItemClickListener(listener); + } } }