问题
How do I prevent an item from being selected in a List? Let's say you want to use it for display or other reasons.
回答1:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Solution 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Call preventDefault in the changing handler like so:
<s:List id="list" dataProvider="{myCollection}" changing="list_changingHandler(event)"/>
The List change handler:
protected function list_changingHandler(event:IndexChangeEvent):void {
var item:Object = list.dataProvider.getItemAt(event.newIndex);
event.preventDefault();
}
The event.preventDefault(); prevents the item from being selected. The code on the line before allows you to get the item that was going to be selected if you are using an ArrayCollection. It may be slightly different for other types of data lists or collections.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Solution 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also prevent an item from being selected in the item renderer by calling the stopPropagation method on the mouseDown event like so:
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" >
<s:CheckBox id="enabledCheckbox" mouseDown="event.stopPropagation();"/>
</s:ItemRenderer>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Solution 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@RIAstar mentioned set enabled to false in the ItemRenderer.
来源:https://stackoverflow.com/questions/12254718/how-to-prevent-an-item-from-be-selected-in-a-list