如何为一个事件设置多个监听器?

发布于 2024-10-28 04:04:48 字数 56 浏览 1 评论 0原文

我想为一个事件设置多个监听器,并且发现使用复合监听器是关键。

谁能给我举个例子吗?

I want to set up multiple listeners for one event, and have found that using composite listener is the key.

Could anyone give me an example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠我空喜 2024-11-04 04:04:48
class CompositeListener implements OnEventListener {
   private List<OnEventListener> registeredListeners = new ArrayList<OnEventListener>();

   public void registerListener (OnEventListener listener) {
      registeredListeners.add(listener);
   }

   public void onEvent(Event e) {
      for(OnEventListener listener:registeredListeners) {
         listener.onEvent(e);
      }
   }
}

……

CompositeListener composite = new CompositeListener();
composite.registerListener(listener1);
composite.registerListener(listener2);
component.setOnEventListener(composite);
class CompositeListener implements OnEventListener {
   private List<OnEventListener> registeredListeners = new ArrayList<OnEventListener>();

   public void registerListener (OnEventListener listener) {
      registeredListeners.add(listener);
   }

   public void onEvent(Event e) {
      for(OnEventListener listener:registeredListeners) {
         listener.onEvent(e);
      }
   }
}

.....

CompositeListener composite = new CompositeListener();
composite.registerListener(listener1);
composite.registerListener(listener2);
component.setOnEventListener(composite);
心清如水 2024-11-04 04:04:48

你可以尝试这个技巧
您将需要扩展视图,例如扩展按钮以接受多个 onclick 侦听器。
实现 OnclickListener 的内部类

private class OnclickListenerCollection implements OnClickListener{
    private ArrayList<OnClickListener> listeners;

    public void addOnclickListener(OnClickListener listener){
        listeners.add(listener);
    }

    @Override
    public void onClick(View v) {
        for(OnClickListener l : listeners){
            l.onClick(v);
        }

    }
}

您创建一个在子类按钮的构造函数中

private OnclickListenerCollection listenerCollector;

,您调用 super.setOnclickListener在您的构造函数中传递内部类的实例:

listenerCollector = new OnclickListenerCollection();
super.setOnClickListener(listenerCollector);

集合中

@Override
public void setOnClickListener(OnClickListener l) {

    listenerCollector.addOnclickListener(l);
}

您重写 setOnclickListener 方法以将侦听器添加到我没有的 尝试过,但我相信它会起作用

you can try this hack
you will need to extend the view e.g. extend the Button to accept multiple onclick listeners.
you create an inner class that implements OnclickListener

private class OnclickListenerCollection implements OnClickListener{
    private ArrayList<OnClickListener> listeners;

    public void addOnclickListener(OnClickListener listener){
        listeners.add(listener);
    }

    @Override
    public void onClick(View v) {
        for(OnClickListener l : listeners){
            l.onClick(v);
        }

    }
}

in the constructor of you sub-classed button, you call super.setOnclickListener passing an instance of you inner class

private OnclickListenerCollection listenerCollector;

in your constructor you have:

listenerCollector = new OnclickListenerCollection();
super.setOnClickListener(listenerCollector);

you override the setOnclickListener method to add listeners to the collection

@Override
public void setOnClickListener(OnClickListener l) {

    listenerCollector.addOnclickListener(l);
}

i have not tried it but i believe it will work

痴情换悲伤 2024-11-04 04:04:48

我知道已经晚了几年,但想添加我的版本,我刚刚花了几个小时开始工作。以下是位于文件 DBupdateEventListener.java 中的代码:

public interface DBupdateEventListener {
    public void onUpdateChannelSubscriptions();
}
class DBupdateEventListenerRegistry implements DBupdateEventListener{
    public static List<DBupdateEventListener> registeredListeners = new ArrayList<DBupdateEventListener>();
    public void registerListener(DBupdateEventListener listener){
        registeredListeners.add(listener);
    }
    public void onUpdateChannelSubscriptions(){
        for (DBupdateEventListener listener:registeredListeners){
            listener.onUpdateChannelSubscriptions();
        }
    }
}

registeredListeners 是一个公共静态变量,这一点非常非常重要。这就是我在这个问题已经关闭几年后添加自己的答案的全部原因。

这是我在触发事件后需要更新的片段的 OnCreate() 方法中使用的代码:

        dbUpdater = new DBupdateEventListener() {
            @Override
            public void onUpdateChannelSubscriptions() {
                //put the code you want to run when the event is triggered here:
                loadChannelListsFromDB();
            }
        };
        DBupdateEventListenerRegistry temp = new DBupdateEventListenerRegistry();
        temp.registerListener(dbUpdater);

这是我在异步任务中首先触发事件的代码:

dbUpdaterRegistry = new DBupdateEventListenerRegistry();
dbUpdaterRegistry.onUpdateChannelSubscriptions();

I know it is several years late, but wanted to add my version that I just spent a few hours getting to work. Here is the code located in the file DBupdateEventListener.java:

public interface DBupdateEventListener {
    public void onUpdateChannelSubscriptions();
}
class DBupdateEventListenerRegistry implements DBupdateEventListener{
    public static List<DBupdateEventListener> registeredListeners = new ArrayList<DBupdateEventListener>();
    public void registerListener(DBupdateEventListener listener){
        registeredListeners.add(listener);
    }
    public void onUpdateChannelSubscriptions(){
        for (DBupdateEventListener listener:registeredListeners){
            listener.onUpdateChannelSubscriptions();
        }
    }
}

it is very VERY important that registeredListeners is a public static variable. This is the whole reason that I added my own answer to this question several years after it had already been closed.

and here is the code I use in the OnCreate() method of the fragment that needs to be updated after the event is triggered:

        dbUpdater = new DBupdateEventListener() {
            @Override
            public void onUpdateChannelSubscriptions() {
                //put the code you want to run when the event is triggered here:
                loadChannelListsFromDB();
            }
        };
        DBupdateEventListenerRegistry temp = new DBupdateEventListenerRegistry();
        temp.registerListener(dbUpdater);

and here is the code used in my asynchronous task that triggers the event in the first place:

dbUpdaterRegistry = new DBupdateEventListenerRegistry();
dbUpdaterRegistry.onUpdateChannelSubscriptions();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文