问题
I'm building a mobile app, using spark list control. When user tap on a list item, i want it to navigate to that correspondence page
<s:List id="list" width="100%" height="100%" labelField=" label">
<s:dataProvider>
<s:ArrayList>
<fx:Object label="Page One" />
<fx:Object label="Page Two" />
<fx:Object label="Page Three" />
<fx:Object label="Page Four" />
</s:ArrayList>
</s:dataProvider>
</s:List>
how do i code it so that when user tap on item label="Page One" it will navigate to PageOneView.mxml and if it tap on "Page Two" it will go to PageTwoView.mxml and so on. thanks
回答1:
The List should dispatch a change event. In that change event handler you can push the new view onto the view stack:
if(list.selectedIndex == 0){
navigator.pushView(viewOne);
} else if (list.selectedIndex == 1){
navigator.pushView(viewtwo);
} else if
etc...
回答2:
I like the switch statement here as you can default to an action,
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title="HomeView">
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
protected function list_changeHandler(event:IndexChangeEvent):void
{
switch(list.selectedItem.label)
{
case "Page One":
{
navigator.pushView(pageOne);
break;
}
case "Page Two":
{
navigator.pushView(pageTwo);
break;
}
case "Page Three":
{
navigator.pushView(pageThree);
break;
}
case "Page Four":
{
navigator.pushView(pageFour);
break;
}
default:
{
navigator.pushView(pageOne);
break;
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:List id="list" width="100%" height="100%" change="list_changeHandler(event)"
labelField="label">
<s:dataProvider>
<s:ArrayList>
<fx:Object label="Page One"/>
<fx:Object label="Page Two"/>
<fx:Object label="Page Three"/>
<fx:Object label="Page Four"/>
</s:ArrayList>
</s:dataProvider>
</s:List>
</s:View>
Just a different way, suits my logic, created in Flash Builder 4.6.
Jacko
来源:https://stackoverflow.com/questions/7153068/how-to-navigate-in-spark-list-control