How to prevent an item from be selected in a List

a 夏天 提交于 2019-12-25 04:54:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!