单个微调器上的多个onItemSelected侦听器

时间:2017-01-24 05:18:11

标签: java android

我有一个微调器,它附有一个基本的onItemSelected侦听器。现在我想要的是向这个微调器添加多个其他onItemSelected侦听器,我希望所有这些都能执行。如何实现呢?

1 个答案:

答案 0 :(得分:2)

将此设置为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...);
        }
    }
}