android 长按 ListView 无法弹出 ContextMeun
可能的原因:onItemLongClick 消费了长按事件
onItemLongClick 消费了长按事件
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
mApSelect = position;
Log.d(TAG, "onItemLongClick:" + mApSelect);
return false;
}
});
关键在 onItemLongClick 函数的返回值,看文档注释:
/**
* Callback method to be invoked when an item in this view has been
* clicked and held.
*
* Implementers can call getItemAtPosition(position) if they need to access
* the data associated with the selected item.
*
* @param parent The AbsListView where the click happened
* @param view The view within the AbsListView that was clicked
* @param position The position of the view in the list
* @param id The row id of the item that was clicked
*
* @return true if the callback consumed the long click, false otherwise
*/
如果返回 ture,系统认为用户实现的回调函数已经处理完了长按事件。而弹出 ContextMenu 也是长按事件的响应,且在 onItemLongClick 响应之后,所以返回 true 导致 ContextMenu 无法弹出。