Onclick event is fired even with a long press

I am using ExpandableListView lv . This is what I have. ExpandableListView lv = (ExpandableListView) findViewById (....); lv.setOnChildClickListener (new ExpandableListView.OnChildClickListener () {@Override public boolean onChildClick (ExpandableListView parent, View v, int gp, int cp, long id) {

  Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show(); //perform action return true; } }); lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu contextMenu, View v,ContextMenuInfo menuInfo) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo; customMenu.show(v); //do other stuff contextMenu=null; } }); 

When I click on the clild element for a long time, customMenu.show(v) is called, and when I turn off the finger, its OnClickListener . Similarly, with a long press and then release the finger on a group element, its ContextmenuListener is called, and then the group expands to show the children. Is this normal behavior? how can i prevent this?

I really want to do things on long Click in a list item. Returning true in longClickListener working correctly (expends the click event). But I also need to get the id, group and child position element, which is provided through ContextMenuInfo only in the context listener.

+4
source share
2 answers

Set a global boolean value like

 boolean isLongClick = false; public void onClick(View arg0) { if(isLongClick == false){ // this checks to see if it was long clicked // Perform your action here } isLongClick = false; // resetting longClick to false after bypassing action } public boolean onLongClick(View arg0) { isLongClick = true; //perform other action here return false; } 

In this case, two action listeners are executed, one for clicks and one for a long click. if the user clicks on the object for a long time, it sets the boolean value to true, thereby blocking the action from being performed in onClickListener, but onClickListener still works no matter what, so that we provide a reset to the boolean value in this method, so after a long press the item will accept one click again.

Provided this tacitly means that you will need to get the item identifier, etc. But this methodology works like a charm. I just implemented it in a popup menu application. I wanted two different menus to appear, depending on whether the user clicked or pressed the anchor for a long time. Thus, if they quickly pressed, it would pop up the (open from) menu, if it was pressed for a long time, it would bring up a menu for (share, edit, delete), etc.

-1
source

Make sure that

 @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { return true; //<-- this should be TRUE, not FALSE } 

returns true . return false seems to continue the call method in onClick() .

This solution worked for me, at least. return false was the default for me when I automatically generated code in eclipse, and I did not think to change it.

+5
source

All Articles