问题
I have 2 tabs one is Parent tab and other is child tabs.I have created Parent tab and put listener using onClose event dynamically to it and then creating child tabs. Actually I want that when user clicks on close button of Tab, he is not able to close and get message so I put event.stopPropogation() to handle the close event. After creation of child tabs, event listener should be removed out from parent tab.But listener is not removing from parent tab. As I am using removeEventlistener but it is also not working.
First Time I am calling a method in which I am adding event listener to the parent tab.
mainTab.getSelectedTab().addEventListener(Events.ON_CLOSE,
new EventListener<Event>() {
public void onEvent(Event event) throws Exception {
event.stopPropogation();
showWarning(message);
return;
}
});
then after creating all child Tabs I have to remove this listener.I am using...
mainTab.getSelectedTab().removeEventListener(Events.ON_CLOSE,
new EventListener<Event>() {
public void onEvent(Event event) throws Exception {
}
});
this listener is not working on this tab but when I open new tab (sibling of main tab) then listener is not called.
How to remove listener from current tab?
Can anyone solve my problem?
回答1:
Thanks for the example, that helped a lot.
The problem here was in the use of the removeEventListener
function. The second argument of the function, an EventListener
instance, is actually the exact event listener that will be removed. You can see this in the ZK source code; the removeEventListener
function is implemented in AbstractComponent
and on line 2140 it checks for a known EventListener
which equals
the function argument.
Here is a working fix:
public class Controller extends SelectorComposer<Window> {
private static final EventListener<Event> EVENT_STOPPER = new EventListener<Event>() {
public void onEvent(Event event) throws Exception {
event.stopPropagation();
System.out.println("Stopped propagation of " + event);
}
};
@Wire
private Tabbox mainTab;
@Override
public void doAfterCompose(Window comp) throws Exception {
super.doAfterCompose(comp);
addCloseEventStopper();
}
@Listen(Events.ON_CLICK + " = #addTabsButton")
public void addTabsButtonClicked() {
removeCloseEventStopper();
addTabs();
}
private void addCloseEventStopper() {
mainTab.getSelectedTab().addEventListener(Events.ON_CLOSE, EVENT_STOPPER);
}
private void removeCloseEventStopper() {
mainTab.getSelectedTab().removeEventListener(Events.ON_CLOSE, EVENT_STOPPER);
}
private void addTabs() {
Tabs tabs = mainTab.getTabs();
tabs.appendChild(new Tab("Tab Two"));
tabs.appendChild(new Tab("Tab Three"));
}
}
The key thing here is that the same EventListener
instance is used in addEventListener
and removeEventListener
.
Note here we've used a private
static
final
inner class, this is just one way to hold on to a reference to an EventListener
. There are other ways depending on your use case.
回答2:
Basically you can call tab.setClosable(false) to remove the 'x' (close button), if you want make a tab not closable with the 'x', you can stop the close action from client side with setWidgetOverride
a zul sample below, you can move setWidgetOverride to anywhere that you can receive instance of that tab
<zk>
<tabbox>
<tabs>
<tab label="unclosable tab" closable="true">
<attribute name="onCreate">
self.setWidgetOverride("_doCloseClick", "function(evt) { return; }");
</attribute>
</tab>
</tabs>
</tabbox>
</zk>
来源:https://stackoverflow.com/questions/14372849/zk-remove-event-listener-from-selected-tab