2

Spark List の項目を変更します。リストの dataProvider をソートしたままにしているため、アイテムはリスト内の別のインデックスに移動します。ただし、selectedIndex はアイテムがあった場所にとどまります。List の 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 に答える