Domanda

I have a listview. What I would like to achieve is that when I longclick any item it will show a dialog menu. However I have a click listener as well. it gets triggered after long click. How Can I cancel that or is there a way of ignoring clcik action for longclick.

Thanks

È stato utile?

Soluzione

Check this out:

There is a specific method for setOnLongClickListener. Just make sure that you setLongClickable to be true for your ListView! You need to separate the listeners you define for a normal click and a long click.

Other than that, try posting some of your code. It will allow the community to better help you.

Altri suggerimenti

My guess is that you are setting the click listener directly on each of the views corresponding to the items in the list, probably in the getView method of your ListAdapter with a code similar to this :

itemView.setOnClickListener(new OnClickListener()
{
   @Override
   public void onClick(final View v)
   {
      // DO SOMETHING
   }
 });

If you do this, the OnClickListeners on these views override the listener set at the ListView level (with registerForContextMenu(listView)) and your context menu will never get called on a long click.

The right way is to set your menu (with long click) and your click listener both at the ListView level :

registerForContextMenu(listView);
listView.setOnItemClickListener( 
    new OnItemClickListener() {        
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long l) {
            // DO SOMETHING 
            // selected item is a.getAdapter().getItem(position);
    }
    });

PS : I'm answering because I had the same issue and I feel the accepted answer with setOnLongClickListener is probably not the recommended way to do this if you need a contextual menu.

If you want to do this the right way, read up on Context Menu. Otherwise you can do it the easy but messy way using pop-up dialogs and by overriding listview's onItemLongClickListener

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top