我有运行微调器的OnItemSelectedListener
事件的代码。因此,当我使用该方法时:
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// I want to do something here if it's a user who changed the the selected item
}
...我怎么知道项目选择是通过编程还是通过用户界面进行的用户操作?
最佳答案
我不知道这可以与方法中的区别。确实,很多人都面临着一个问题,即启动微调器时会触发onItemSelected
。目前看来,唯一的解决方法是为此使用外部变量。
private Boolean isUserAction = false;
...
public void onItemSelected( ... ) {
if( isUserAction ) {
// code for user initiated selection
} else {
// code for programmatic selection
// also triggers on init (hence the default false)
}
// reset variable, so that it will always be true unless tampered with
isUserAction = true;
}
public void myButtonClick( ... ) {
isUserAction = false;
mySpinner.setSelectedItem ( ... );
}