listview中长按事件响应

在Android的菜单分两种:options menu 、context menu 。(也有分三种的:options menu 、context menu、sub menu)其中options menu是和Activity相关联的,context menu 是和View关联。我们需要点击控件出现的菜单属于context menu ;但是context menu 只能通过setOnCreateContextMenuListener来进行关联。 
     实现这个效果需要理解对ListView的item长按之后发生的事情:
    首先触发的是onItemLongClick函数,执行onItemLongClick操作,然后根据onItemLongClick的返回值判断是不是执行OnCreateContextMenuListener函数;如果onItemLongClick返回false,则会继续执行OnCreateContextMenuListener函数,否则返回true,忽略OnCreateContextMenuListener函数。
    因此我们可以在onItemLongClick的时候进行判断并且准备好OnCreateContextMenuListener中需要用到的数据,然后在OnCreateContextMenuListener中调用这些数据显示即可。

你可能感兴趣的:(listview中长按事件响应)