In my case, I have a ListView
which contains two kinds of views, folder开发者_开发知识库s and images. When long press on a folder, a ContextMenu
with 2 items "Enter the folder" and "Bookmark the folder" shows up, and when for images, a ContextMenu
with "View" and "Delete" shows up.
Generally, I should just override onContextItemSelected
in Activity
and list all possible menu item IDs in a switch-case. But actions on folders and actions on images are two completely unrelated things, right? Is that strange to put those two (or even more) things in one function?
I wonder, is there a way that I could set callback handlers to each context menu instead of messing up onContextItemSelected
? Any advice is appreciated.
I think using ContextMenu is neither flexible nor convenient decision at all.
Yes, it is strange to process 10 or more different actions in one method, so maybe it's better use AlertDialogs instead of ContextMenu. Look at this: http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
Looks like context menu, right? You can show dialog in your element's OnLongClickListener or in AdapterView.OnItemLongClickListener (in case of ListView), and you can process actions in DialogInterface.OnClickListener.
Also look at the question I asked, perhaps it will be helpful for you too: How to create dialog that looks exactly like ContextMenu?
In your onCreateContextMenu()
-method, you can determine if a folder or an image was clicked by checking the passed View
-parameter.
After that, you could create a separate menu for your folders and one for your images (a different XML-file for every menu) and inflate it.
In the onContextItemSelected()
-method, you then simply check for all of the possible entry's (no madder which menu got loaded). Since you only have four different possible menu-items, this will not be too bad.
精彩评论