2009-08-14 45 views
0

以下のコードでは、presentAlbumIndexを使用してTileListのselectedIndexを制御します。項目「5」が最初に選択される。ボタンが押されるたびに、配列の最初の項目が削除され、presentAlbumIndexがデクリメントされます。TileList selectedIndexが更新されないのはなぜですか?

理論的には、選択されたインデックスは、ボタンがクリックされるたびに(「5」自体が削除されるまで)、「5」のままでなければなりません。これは、最初のボタンを押すときにこのように動作します。しかし、2番目のボタンを押すと、何らかの理由でハイライト表示が「6」に変わります。また、TileListのselectedIndexは常に1になります。

なぜですか?

ListBaseを調べ、selectedIndexを監視しようとしました。 selectedIndex は、最初はが正しいインデックスに更新されていますが、ある時点で正しいインデックス+1に戻っているようです。なぜそれが戻っているのか分かりません。

同じ操作でデータプロバイダの削除とインデックスの変更を行っているためです。

TileListにはいくつかの機能がありますが、selectedIndexを最新の状態に保つためにオーバーライドできますか?

スティーブ

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
     layout="absolute" 
     applicationComplete="init()"> 
    <mx:Label id="label1" text="{'TileList selected index: ' + albumsThumbnailList.selectedIndex}" 
     x="255" y="55" width="234"/> 
    <mx:Label id="label2" text="{'Presented album index: ' + presentedAlbumIndex}" x="255" y="81" width="234"/> 
    <mx:TileList id="albumsThumbnailList" direction="vertical" 
     dataProvider="{presentedAlbums}" 
     selectedIndex="{presentedAlbumIndex}" x="25" y="13" 
     change="presentedAlbumIndex=albumsThumbnailList.selectedIndex" 
     height="400"/> 
    <mx:Button click="test2()" x="297" y="150"/> 

    <mx:Script> 
     <![CDATA[ 
      import mx.events.CollectionEvent; 
      import mx.collections.ArrayCollection; 
      private var _includedAlbums:ArrayCollection = new 
       ArrayCollection(["zero","one","two","three","four","five","six","seven"]); 

      [Bindable] 
      private var presentedAlbumIndex:int = 5; 

      private function init():void { 

       _includedAlbums.addEventListener(CollectionEvent.COLLECTION_CHANGE, 
        function():void { 
         dispatchEvent(new Event("albumDataChanged")); 
        } 
       ); 
      } 

      public function test2():void { 
       _includedAlbums.removeItemAt(0); 
       presentedAlbumIndex--; 
      } 

      [Bindable(event="albumDataChanged")] 
      public function get presentedAlbums():ArrayCollection { 
       return _includedAlbums; 
      } 

     ]]> 
    </mx:Script> 

</mx:Application> 

答えて

0

COLLECTION_CHANGEDリスナーで、これはそれがselectedIndexの上で結合した後に発火する非同期イベントですので、リストには、新しいのdataProviderを取得します。このリスナーを削除し、コレクションが変更されるたびに新しいコレクションdataProviderを提供しないことにより、selectedIndexバインディングは期待どおりに機能します。

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
     layout="absolute" 
     applicationComplete="init()"> 
    <mx:Label id="label1" text="{'TileList selected index: ' + albumsThumbnailList.selectedIndex}" 
     x="255" y="55" width="234"/> 
    <mx:Label id="label2" text="{'Presented album index: ' + presentedAlbumIndex}" x="255" y="81" width="234"/> 
    <mx:TileList id="albumsThumbnailList" direction="vertical" 
     dataProvider="{presentedAlbums}" x="25" y="13" 
     selectedIndex="{presentedAlbumIndex}" 
     height="400"/> 
    <mx:Button click="test2()" x="297" y="150"/> 

    <mx:Script> 
     <![CDATA[ 
      import mx.events.CollectionEvent; 
      import mx.collections.ArrayCollection; 
      private var _includedAlbums:ArrayCollection = new 
       ArrayCollection(["zero","one","two","three","four","five","six","seven"]); 

      [Bindable] 
      private var presentedAlbumIndex:int = 5; 

      private function init():void { 

      } 

      public function test2():void { 
       _includedAlbums.removeItemAt(0); 
       presentedAlbumIndex--; 
      } 

      [Bindable(event="albumDataChanged")] 
      public function get presentedAlbums():ArrayCollection { 
       return _includedAlbums; 
      } 

     ]]> 
    </mx:Script> 

</mx:Application> 
+0

いいえ、インデックスを減らす前にrefresh()を呼び出すと、同じ問題が発生します。私は実際のアプリではTileListが別のクラスにあるので、_includedAlbumsに直接バインドしません。 –

+0

COLLECTION_CHANGEDリスナーを削除すると、正しく機能します。それは必須ですか? –

+0

私の場合、COLLECTION_CHANGEが必要です。私は、COLLECTION_CHANGEイベントを使用して、データプロバイダ階層の深い内部のアクティビティをリスナーに通知しています。私のアプリケーションでは、実際には、他のバインディングもpresentAlbumsに設定されていて、selectedIndexの問題はすべて更新されません。 COLLECTION_CHANGEイベントは非同期で、その後発生します。なぜselectedIndexがデクリメントされてから再インクリメントされるのかを説明しています。 ListBaseでデータプロバイダの再インクリメントが発生するのはどのような考えですか?たぶん、選択したインデックスを更新しないようにオーバーライドすることができます。 –

関連する問題