Show context menu on demand in Android
Consider an Android ListView
with a context menu set up with registerForContextMenu
. The context menu of a ListView
item is shown when the user long taps over the item view.
Additionally, I would like to show the context menu when the user taps (not long taps) on the item (if certain conditions are met). Is it开发者_开发知识库 possible to do this? How?
I managed to solve this by browsing the Android source code. Here's what I did:
protected void onItemClick(AdapterView<?> adapterView, int position) {
final int start = adapterView.getFirstVisiblePosition();
final int index = position - start;
final View childView = adapterView.getChildAt(index);
if (childView != null) {
adapterView.showContextMenuForChild(childView);
}
}
Yes call performLongClick method from your click event and it should do it
精彩评论