我有一个带有基本onItemSelected侦听器的微调器。现在,我要向此微调器添加多个其他onItemSelected侦听器,我希望它们全部执行。如何执行呢?
最佳答案
将此设置为Spinner的侦听器,然后将其他侦听器添加到此侦听器:
class ItemSelectedMultiListener implements OnItemSelectedListener {
private List<OnItemSelectedListener> mListeners = new ArrayList<>();
public void addListener(OnItemSelectedListener listener) {
mListeners.add(listener);
}
public void removeListener(OnItemSelectedListener listener) {
mListeners.remove(listener);
}
@Override
public void onItemSelected(...args...) {
for (OnItemSelectedListener listener : mListeners) {
listener.onItemSelected(...args...);
}
}
@Override
public void onNothingSelected(...args...) {
for (OnItemSelectedListener listener : mListeners) {
listener.onNothingSelected(...args...);
}
}
}