Flex 中继器和事件监听器 [英] Flex Repeaters and EventListeners

查看:31
本文介绍了Flex 中继器和事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乍一看,Repeater 控件看起来非常强大,但现在我有了第二个想法.

At first glance, the Repeater control looks extremely powerful, but now I'm having second thoughts.

手头的问题:给孩子添加事件监听器

Problem at hand: Adding Event Listeners to the children

Repeater 对象绑定到并遍历 ArrayCollection,为每个项目创建新的 Vbox 子项.现在对于每个孩子,Repeater 对象将触发重复"事件,我很想在其中添加事件侦听器(用于鼠标事件).到目前为止没有问题,但是,当 ArrayCollection 更改时会发生什么,我应该如何删除所有老孩子的 EventListeners?是否有一组包含我在文档中跳过的 Vbox 实例的子项?当事件侦听器所连接的对象被销毁时,它们是否会被很好地清理?

Repeater object is bound to and iterates through an ArrayCollection creating new Vbox children for each item. Now for each child, the Repeater object will fire off a "repeat" event, where I'm tempted to add the eventlistener (for mouse events). No problems so far, but, what happens when the ArrayCollection changes, how should I remove the EventListeners for all the old children? Is there an array of children containing my Vbox instances that I'm skipping over in the docs? Are eventlisteners cleaned up nicely when the object they are attached to is destroyed?

-C 编码器在 flex/actionscript 中丢失

-C coder lost in flex/actionscript

推荐答案

我会完全避免使用中继器.从性能的角度来看,它们非常慢,因为这些项目经常被销毁和重新创建.最好使用基于列表的控件并实现 itemRenderer.

I would avoid using Repeaters entirely. From a performance standpoint they are very slow because the items are frequently destroyed and recreated. You are better off using a List-based control and implementing an itemRenderer.

默认情况下,事件侦听器是强引用,因此如果您不删除事件侦听器,它将阻止对象被垃圾收集.您可以在名为weakReference"的 addEventListener 中使用可选的第 5 个参数并将值设置为 true 以添加不会阻止 GC 的事件侦听器.

Event listeners by default are strong references, so if you do not remove your event listeners it will prevent the object from being garbage collected. You can use the optional 5th parameter in the addEventListener called "weakReference" and set the value to true to add an event listener that will not prevent GC.

为了更好地处理 itemRender 组件中的事件,您可以实现 IDropInListItemRenderer 接口.这将使您可以访问具有所有者"属性的listData",该属性是实际的 List 对象本身.在您的 itemRenderer 中,通过所有者调度包含必要数据的自定义事件.如果在创建 List 控件后向其添加事件侦听器,则可以在包含该控件的组件中进行事件处理.

To better deal with events in your itemRender component, you can implement the IDropInListItemRenderer interface. This will give you access to "listData" which has an "owner" property which is the actual List object itself. In your itemRenderer, dispatch a custom Event containing the necessary data through the owner. If you add an event listener to the List control after it's created then you can do the event handling in the component containing the control.

这篇关于Flex 中继器和事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆