How can I catch such event? onCreateContextMenu
is quite similiar, but I don't need menu.
It's hard to know what you need to achieve. But my guess is that you want to perform some acion over the item that receives the long click. For that, you have two options:
.
listView.setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
//do your stuff here
}
});
Normally, you'd associate a long click on a list view with a context menu, which you can do by registering the listView with Activity.registerForContextMenu(View view)
to give a more consistent user interface experience with other android apps.
and then override the onContextItemSelected method in your app like this:
@Override
public void onCreate(Bundle savedInstanceState) {
listView = (ListView) findViewById(R.id.your_list_view);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(getString(R.string.menu_context_title));
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.some_item:
// do something useful
return true;
default:
return super.onContextItemSelected(item);
}
The position in the list is also held in info.id
If you just want to capture the long click event, then I think what Snicolas is suggesting would work.
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