0

在下面的代码中,presentAlbumIndex 用于控制 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>
4

1 回答 1

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>
于 2009-08-14T01:23:56.703 回答