即使长按也可以执行Onclick事件

arn*_*321 4 android listview

我正在使用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;
    }
});
Run Code Online (Sandbox Code Playgroud)

当我长时间点击一个子项时,customMenu.show(v)会调用它,当我抬起手指时,它会OnClickListener被调用.类似地,在长按并然后在组项目上释放手指时,将ContextmenuListener调用它,然后该组将展开以显示子项目.这是正常的行为吗?我该如何防止这种情况?

我其实想long Click在列表项上做些什么.回到truelongClickListener工作正常(消耗click事件).但我也需要得到项目的ID,组和儿童的位置,这是通过提供ContextMenuInfocontextmenu唯一听众.

sco*_*eus 6

确保这件事

@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {

        return true; //<-- this should be TRUE, not FALSE   
    }
Run Code Online (Sandbox Code Playgroud)

正在恢复true.返回false似乎继续调用的方法onClick().

这个解决方案至少对我有用.return false当我在eclipse中自动生成代码时,我是默认设置,我没想过要改变它.