Prevent selection of a particular item in spark list

元气小坏坏 提交于 2019-12-11 01:54:39

问题


I have a Spark List which has a custom itemRenderer for rendering each item in the List. I wish to prevent an item in that list from being selected (based on some custom logic) by the user.

What is the best way I can achieve this?

Here's how my List is defined:

<s:List id="myList" itemRenderer="com.sample.MyItemRenderer" />

and of course, I have a item renderer defined as the class com.sample.MyItemRenderer.


回答1:


The selection of items is handled by the list alone as far as I know, so I would say that you can manage it from there. I would have a field on the Objects that are in the list called "selectable" or something like that and when the list item is changing check to see if the new item is actually selectable and if it isn't then you can either have it clear the selection or reset to the previous selection. You can accomplish that by reacting to the "changing" event on the list component and calling "preventDefault" on the IndexChangeEvent as follows:

protected function myList_changingHandler(event:IndexChangeEvent):void {
    var newItem:MyObject = myList.dataProvider.getItemAt(event.newIndex) as MyObject;
    if(!newItem.selectable) {
        event.preventDefault();
    }
}

// Jumping ahead ...

<s:List id="myList" changing="myList_changingHandler(event)" // ... continue implementation

The relevant part of the MyObject class is as follows:

public class MyObject {

    private var _selectable:Boolean;

    public function MyObject(){

    }

    public function set selectable(value:Boolean):void {
        _selectable = value;
    }

    public function get selectable():Boolean {
        return _selectable;
    }
}


来源:https://stackoverflow.com/questions/7539185/prevent-selection-of-a-particular-item-in-spark-list

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