我正在使用 ExpandableListView lv
。这就是我所拥有的。
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;
}
});
当我长按一个子项时,customMenu.show(v)
被调用,当我松开手指时,它的 OnClickListener
被调用。
同样,在组项目上长按然后松开手指时,将调用其 ContextmenuListener
,然后组展开以显示子项目。这是正常行为吗?我该如何防止这种情况?
我实际上想在列表项上长按
时做一些事情。在 longClickListener
上返回 true
可以正常工作(消耗点击事件)。
但我还需要获取项目的 ID、组和子位置,这些信息仅通过 contextmenu
监听器中的 ContextMenuInfo
提供。
最佳答案
确保
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
return true; //<-- this should be TRUE, not FALSE
}
正在返回 true
。返回 false
似乎继续调用 onClick()
的方法。
这个解决方案至少对我有用。 return false
是我在 eclipse 中自动生成代码时的默认设置,我没想过要更改它。
关于android - onclick 事件即使在长按时也会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113187/