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 Answers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.

- 16,517
- 32
- 123
- 231
-
Seems to me it would be easier to just set the ItemRenderer's `enabled` property to `false` – RIAstar Sep 04 '12 at 09:13
-
1Setting the ItemRenderer's enabled property to false makes all interaction with the list item inactive. If one wishes to interact with the content but not have the list item be highlighted/focused/selected, this is not an option. – Thistledowne Apr 03 '13 at 19:20