今天在做demo時,須要設置ListView的item的長按和點擊事件。OnItemLongClickListener和OnItemClickListener,然而點擊事件能夠實現,可是在長按操作時會同一時候觸發點擊事件(長按和點擊沖突),然后查看了一下Android的相關源代碼,能夠看到系統是優先對應clicklisener的,他是沒有返回值的的。而對於OnItemLongClickListener是有一個返回值標識。
對於一次長按操作。假設返回false,則2個lisener都會對應,假設返回true則系統僅僅處理長按事件。
因此。想要自己的OnItemLongClickListener長按事件生效,須要將返回值設為true。
以下是源代碼
/**
* Interface definition for a callback to be invoked when an item in this
* AdapterView has been clicked.
*/
public interface OnItemClickListener {
/**
* Callback method to be invoked when an item in this AdapterView has
* been clicked.
* <p>
* Implementers can call getItemAtPosition(position) if they need
* to access the data associated with the selected item.
*
* @param parent The AdapterView where the click happened.
* @param view The view within the AdapterView that was clicked (this
* will be a view provided by the adapter)
* @param position The position of the view in the adapter.
* @param id The row id of the item that was clicked.
*/
void onItemClick(AdapterView<?> parent, View view, int position, long id);
}
/**
* Interface definition for a callback to be invoked when an item in this
* view has been clicked and held.
*/
public interface OnItemLongClickListener {
/**
* 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
* 源代碼這里已經給出解釋。假設返回值設為true。則系統消耗掉長按事件
* @return true if the callback consumed the long click, false otherwise
*/
boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id);
}
