標籤:
可能的原因: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 無法彈出。
來自為知筆記(Wiz)
android 長按 ListView 無法彈出 ContextMeun