/**
* <p>A spinner does not support item click events. Calling this method
* will raise an exception.</p>
* <p>Instead use {@link AdapterView#setOnItemSelectedListener}.
*
* @param l this listener will be ignored
*/
@Override
public void setOnItemClickListener(OnItemClickListener l) {
throw new RuntimeException("setOnItemClickListener cannot be used with a spinner.");
}
Spinner源码中setOnItemClickListener说到,切换使用setOnItemSelectedListener。
原因:
Spinner的父类AbsSpinner源码中并没有实现任何其父类AdapterView的任何类似点击事件,所以所有事件都交给Spinner去实现,这里禁用了setOnItemClickListener事件,点击事件还剩下
setOnClickListener(View.OnClickListener l)
setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener)
setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)
这些事件可用,其中长点击事件不用。查看AdapterView的setOnClickListener事件源码
@Override
public void setOnClickListener(OnClickListener l) {
throw new RuntimeException("Don't call setOnClickListener for an AdapterView. "
+ "You probably want setOnItemClickListener instead");
}
可以看到该方法也被禁用了,提示我们切换setOnItemClickListener,但setOnItemClickListener方法在Spinner被实现并禁用了,那么只剩下onItemSelectedListener没被Spinner实现并且在AdapterView中可用。