I have a ListFragment where I register the context menu using registerForContextMenu() and I override onCreateContextMenu().
The issue is that onCreateContextMenu() is never called when I long press any item.
Here's some code:
public class List_F extends ListFragment {
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
registerForContextMenu(getListView());
setListAdapter(...);
setListShown(false);
// launch cursor loader
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int i = item.getItemId();
if (i == R.id.menu_item_delete) {
delete(info.id);
return true;
} else {
return super.onContextItemSelected(item);
}
}
}
Any idea why?
I also tried
getListView().setOnItemLongClickListener()
and it's not even called.
Any idea?
Found the culprit!
I was setting a OnClickListener on the view in the Adapter.
This was probably eating the long press event.
I changed the OnClickListener in each view to a OnItemClickListener on the ListView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With