如何在Flex中的单个列表中创建多个事件?

时间:2012-01-20 02:34:27

标签: flash flex

有人可以在flex中的单个列表中显示如何创建多个视图更改事件的示例吗?

我希望列表中的每个项目都更改为完全唯一的视图文件。而不是列表中的10个项目都进入1个新视图但生成了不同的信息我想要10个不同的视图,列表中的每个项目1个。

我找不到任何关于如何做到这一点的例子,我一直试图让它成为一段时间。这适用于移动应用。谢谢!

<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
protected function myList_changeHandler(event:IndexChangeEvent):void {
navigator.pushView(views.EmployeeView,myList.selectedItem);
}
protected function myList2_changeHandler(event:IndexChangeEvent):void {
navigator.pushView(views.EmployeeView2,myList.selectedItem);
}
protected function myList3_changeHandler(event:IndexChangeEvent):void {
navigator.pushView(views.EmployeeView3,myList.selectedItem);
}           
]]>
</fx:Script>

<s:List width="100%" height="100%" labelField="firstName">
<s:ArrayCollection>
<fx:Object id="myList" change="myList_changeHandler(event)" firstName="Bill"/>
<fx:Object id="myList2" change="myList2_changeHandler(event)" firstName="Dave"/>
<fx:Object id="myList3" change="myList3_changeHandler(event)" firstName="Mary"/>
</s:ArrayCollection>
</s:List>

1 个答案:

答案 0 :(得分:1)

你在这里缺少一些东西。首先,您需要在List组件上侦听change事件:

<s:List id="myList" width="100%" height="100%" labelField="firstName" change="myListChange(event)">

接下来,您需要创建更改视图的方法:

protected function myListChange( event:IndexChangeEvent ):void
{
    var newView:Class;

    // You can decide a different view based on the newIndex property of the event
    // There could be many better ways than a switch statement - this is for brevity
    switch ( event.newIndex )
    {
        case 0:
            newView = EmployeeView0;
            break;
        case 1:
            newView = EmployeeView1;
            break;
        default:
            newView = OtherEmployeeView;
            break;
    }

    navigator.pushView(newView,myList.selectedItem);
}