Reputation: 1969
I'm working with a custom ListView, which implements dragging elements to reorder them. The reordering is initiated by long click.
I'd also like to open the context menu for each element in the list by a single, short click, like this (code from the fragment containing the list view):
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mainFragment.this.getActivity().openContextMenu(view);
}
});
The problem is, that internally Android apparently opens the context menu by calling the onItemLongClick
method of the view's OnItemLongClickHandler
(source), which obviously causes the dragging behaviour to initiate.
How could I circumvent this behaviour?
Upvotes: 1
Views: 104
Reputation: 1969
I figured it out. You can set a private boolean variable to true in onItemClick, and suppress all behaviour in onItemLongClick, if this boolean variable is true:
setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
itemClicked = true;
parent.showContextMenuForChild(v);
}
});
setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int pos, long id) {
if(!itemClicked) {
//dragging logic here
}
itemClicked = false;
Upvotes: 2