2011-01-19 12 views
2

Spark Listのアイテムを変更します。リストのdataProviderをソートしたままにするので、アイテムはリスト内の別のインデックスに移動します。ただし、selectedIndexは、以前のアイテムの位置にとどまります。私はリストのselectedIndexが変更された項目に依然として存在するようにしたい。誰か前にこの問題を解決したか、ヒントがありますか?Actionscript/Flex:Sparkリストのソート後に選択したアイテムを(インデックスの代わりに)メンテナンスする方法

+0

コード例を投稿できますか? –

+0

あなたはスパークリストコンポーネントのselectedIndices:Vectorに新しいインデックスまたはアイテムを割り当てることはできません。 またはselectedItems:Vector。 のプロパティ – TheDarkIn1978

答えて

1

ありがとうございました。私はついにこれを解決しました。私のスパークリストのサブクラスでは、set dataProviderをオーバーライドし、weak参照イベントリスナーを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; 
      } 
     } 
    } 
関連する問題