Equivalent to PopupMenu in ActionBarSherlock

What is the PopupMenu equivalent in ActionBarSherlock? I can't seem to find him. Its API 11, why is it missing?

+8
android actionbarsherlock
source share
3 answers

The MenuPopupHelper class pretty much does the job. I did not find an easy way to listen for clicks on elements, so I implemented this class, which comes from MenuPopupHelper:

public class MenuPopup extends MenuPopupHelper { OnMenuItemClickListener onMenuItemClickListener; public MenuPopup(Context context, MenuBuilder menu, View anchorView) { super(context, menu, anchorView); } 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); } } 
+11
source share

Added by PopupMenu to ActionBarSherlock .

Styling PopupMenu -

 <item name="popupMenuStyle">@style/PopupMenu.MyAppTheme</item> <style name="PopupMenu.MyAppTheme" parent="@style/Widget.Sherlock.ListPopupWindow"> <item name="android:popupBackground">@android:color/white</item> </style> 
+3
source share

I am working on it now. I did what CommonsWare suggested about his appeal. I basically took the source code of PopupMenu.java and replaced the import of packages with actionbarsherlock equivalents. It seems to work just fine on the gingerbread and ics devices I tested. Although the trick is in the actionbarsherlocks of the MenuPopupHelper class, I had to comment on lines that reference the View_HasStateListenerSupport, for example:

 ((View_HasStateListenerSupport)anchor).addOnAttachStateChangeListener(this); 

for some reason. If I did not, I would get a ClassCastException:

E / AndroidRuntime (9197): FATAL EXCEPTION: main E / AndroidRuntime (9197): java.lang.ClassCastException: android.widget.Button cannot be attributed to com.actionbarsherlock.internal.view.View_HasStateListenerSupport E / AndroidRuntime (9197): at com .actionbarsherlock.internal.view.menu.MenuPopupHelper.tryShow (MenuPopupHelper.java:121) E / AndroidRuntime (9197): at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.show (MenuPopupHelper.java:102)

I'm not sure that commenting on this listener can cause problems for other classes that use MenuPopupHelper or why they throw this exception (possibly an error). But I thought I would share what I tried, so this can help anyone who is looking at it.

+2
source share

All Articles