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?
有人能举个例子吗?
3 个解决方案
#1
44
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);
#2
5
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
你可以试试这个黑客,你需要扩展视图,例如扩展Button以接受多个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);
}
}
}
in the constructor of you sub-classed button, you call super.setOnclickListener passing an instance of you inner class
在你的子类按钮的构造函数中,你调用super.setOnclickListener传递你内部类的一个实例
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
您重写setOnclickListener方法以将侦听器添加到集合中
@Override
public void setOnClickListener(OnClickListener l) {
listenerCollector.addOnclickListener(l);
}
i have not tried it but i believe it will work
我没有尝试过,但我相信它会奏效
#3
1
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:
我知道它已经晚了几年,但是想加上我刚刚上班几个小时的版本。以下是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.
registerListeners是一个公共静态变量非常重要。这就是我在关闭这个问题几年后我自己回答这个问题的全部原因。
and here is the code I use in the OnCreate() method of the fragment that needs to be updated after the event is triggered:
这是我在片段的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);
and here is the code used in my asynchronous task that triggers the event in the first place:
这是我的异步任务中使用的代码,它首先触发事件:
dbUpdaterRegistry = new DBupdateEventListenerRegistry();
dbUpdaterRegistry.onUpdateChannelSubscriptions();
#1
44
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);
#2
5
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
你可以试试这个黑客,你需要扩展视图,例如扩展Button以接受多个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);
}
}
}
in the constructor of you sub-classed button, you call super.setOnclickListener passing an instance of you inner class
在你的子类按钮的构造函数中,你调用super.setOnclickListener传递你内部类的一个实例
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
您重写setOnclickListener方法以将侦听器添加到集合中
@Override
public void setOnClickListener(OnClickListener l) {
listenerCollector.addOnclickListener(l);
}
i have not tried it but i believe it will work
我没有尝试过,但我相信它会奏效
#3
1
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:
我知道它已经晚了几年,但是想加上我刚刚上班几个小时的版本。以下是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.
registerListeners是一个公共静态变量非常重要。这就是我在关闭这个问题几年后我自己回答这个问题的全部原因。
and here is the code I use in the OnCreate() method of the fragment that needs to be updated after the event is triggered:
这是我在片段的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);
and here is the code used in my asynchronous task that triggers the event in the first place:
这是我的异步任务中使用的代码,它首先触发事件:
dbUpdaterRegistry = new DBupdateEventListenerRegistry();
dbUpdaterRegistry.onUpdateChannelSubscriptions();