How do I handle click event in Spark List control in Flex 4
I have a s:List component. I want to handle a click event to know what list item is selected. I don't see Click event in s:List. Any workarounds?
T开发者_运维知识库hanks.
I know I'm late to the party here, but the simplest way to get the selected node from list in a click event is to use the currentTarget property.
function myClickHandler(event:MouseEvent):void{
Alert.show("My Var: " + event.currentTarget.selectedItem.myVar);
}
<s:List ... click="myClickHandler(event);">
...
</s:List>
see:
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7cdb.html
You can use the IndexChangeEvent.CHANGE on List http://docs.huihoo.com/flex/4/spark/events/IndexChangeEvent.html
Package spark.events Class public class IndexChangeEvent Inheritance IndexChangeEvent Event Object
Language Version: ActionScript 3.0 Product Version: Flex 4 Runtime Versions: Flash Player 10, AIR 1.5
The IndexChangeEvent class represents events that are dispatched when an index changes in a Spark component.
See also
spark.components.supportClasses.ListBase spark.components.List spark.components.ButtonBar
I figured how to do this. Thought I would share so that it helps others like me:
<s:List id="taskList" creationComplete="taskList.addEventListener('listClickEvent',handleListClick);" width="100%" height="80%" labelField="description" dataProvider="{todoList}"
useHandCursor="true">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer click="handleClick(event)">
<fx:Script>
<![CDATA[
import ListClickEvent;
import flash.events.MouseEvent;
import mx.controls.Alert;
private function handleClick(me:MouseEvent):void
{
var listClickEvent:ListClickEvent = new ListClickEvent("listClickEvent");
listClickEvent.index = itemIndex;
owner.dispatchEvent(listClickEvent);
}
]]>
</fx:Script>
<s:Label text="{data.description}" top="5" bottom="5" right="3" left="3"/>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
Another way:
<s:List id="myid"
dataProvider="{listDP}"
width="100%"
height="100%"/>
on application creation complete:
myid.addEventListener(MouseEvent.CLICK,clickHandler);
Handler:
private function clickHandler(event:MouseEvent):void
{
if(myid.selectedIndex>=0)
{
...
}
myid.selectedIndex=-1;//to detect click on same item
}
Thanks guys,
Just make sure your List
has its id
variable set. Then you call in your click handler function like this:
private function listClickHandler(event:IndexChangeEvent) {
if(myListsID.seletectedIndex == 0){
navigator.pushView(whateverViewyouwant)
} else if(myListsID.selectedIndex ==1){
navigator.pushView(changetoanotherview)
} else if(myListsID.selectedIndex == 2){
navigator.pushView(mobileViewsareEasy)
} else if(myListsID.selectedIndex == 3){
navigator.pushView(wowSomanyViews)
}
}
The variable that goes into the pushView
function corresponds to the mxml file name for the view you want to load
That is farrrr too complex here is a better way:
<s:List id="whatever" dataProvider="{allMyData}" click="whateverList_click(event)"> </s:List>
<fx:Script>
var whatWasClicked:String = whatever.dataProvider.getItemAt(whatever.selectedIndex).label;
</fx:Script>
Boo ya.
<s:List id="lstDesc" width="100%" height="100%">
<s:change>
Descselected();//do your stuff here
</s:change>
</s:List>
in flash builder.
精彩评论