2

我更改了 Spark List 中的一个项目。然后该项目移动到列表中的不同索引,因为我保持列表的 dataProvider 排序。但是, selectedIndex 保留在项目以前所在的位置。我希望列表的 selectedIndex 仍然在更改的项目上。有没有人解决过这个问题或有任何提示?

4

1 回答 1

1

谢谢大家,我终于解决了这个问题。为了后代,这就是我所做的:

在我的 Spark List 子类中,我重写了 set dataProvider,并将弱引用事件侦听器附加到 dataProvider。

    override public function set dataProvider(theDataProvider:IList):void
    {
        super.dataProvider = theDataProvider;
        dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChange, false, 0, true);
    }

然后,在事件处理程序中,如果之前选择了移动的项目,我会重新选择它。见CollectionEventKind.MOVE案例。

    private function onCollectionChange(theEvent:CollectionEvent):void
    {
        if (theEvent.kind == CollectionEventKind.ADD)
        {
            // Select the added item.
            selectedIndex = theEvent.location;
        }
        else if (theEvent.kind == CollectionEventKind.REMOVE)
        {
            // Select the new item at the location of the removed item or select the new last item if the old last item was removed.
            selectedIndex = Math.min(theEvent.location, dataProvider.length - 1);
        }
        else if (theEvent.kind == CollectionEventKind.MOVE)
        {
            // If the item that moved was selected, keep it selected at its new location.
            if (selectedIndex == theEvent.oldLocation)
            {
                selectedIndex = theEvent.location;
            }
        }   
    }
于 2011-01-31T22:14:41.883 回答