问题
I have a simple LinearLayout with ListView inside it (match_parent
both for width and height). I need a context menu to show up upon a long touch on ANY place in the ListView. First, I called registerForContextMenu
for the ListView. This makes the context menu show up ONLY if the touch is performed on a list item. Then I called registerForContextMenu
for the LinearLayout as well, and set it attribute android:longClickable="true"
. This did not help.
So the question is how can one produce a context menu for a long touch below list items? For example, if we have 1 item in the list view, there is a lot of empty space below the item. Why does it not used for context menu invocation, provided that the list view fills entire parent according to the layout?
回答1:
I don't think that long clicking anywhere on the ListView
to get a ContextMenu
it's a normal behavior, one the user will expect(especially in your case, with the user clicking the empty space below the ListView
and a ListView
ContextMenu
would appear).
Anyway, for your example, you can set your ListView
height attribute to wrap_content
(If I remember right, this is the recommended value) and then register the Contextmenu
for the ListView
and the parent LinearLayout
(of course you'll have to provide the same code for both).
来源:https://stackoverflow.com/questions/10447676/context-menu-for-blank-space-in-a-listview