在下面的代码中,presentsAlbumIndex用于控制TileList的selectedIndex。最初选择项目“五”。每当按下按钮时,数组中的第一项将被删除,并且presentAlbumIndex将递减。
理论上,每次单击按钮时,所选索引应保持“五”(直到“五”本身被删除)。它按照第一次按下按钮的方式工作。但是,在第二个按钮上,由于某种原因按突出显示更改为“六”。此外,TileList selectedIndex总是一个落后。
为什么?
我尝试查看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 :(得分:0)
使用COLLECTION_CHANGED侦听器,List会获取一个新的dataProvider,因为这是一个异步事件,它会在selectedIndex上绑定后触发。通过删除此侦听器并在集合更改时不提供新的集合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>