Spark List SelectedIndex不会更新

时间:2011-09-08 12:27:40

标签: flash flex actionscript-3 data-binding mate

所以我的问题是当我使用nextSlide()和prevSlide()时,selectedIndex没有更新,当我点击两个按钮时执行它们。当我单击列表中的元素时,SelectedIndex工作。我究竟做错了什么?我必须派遣一些活动吗?使用按钮进行操作是否有任何技巧?

这是我的播放列表

public class Playlist extends List
        {
            private var dispatcher:Dispatcher;

            public function Playlist()
            {
                super();

                this.dragEnabled = true;
                this.dragMoveEnabled = true;
                this.dropEnabled = true;
                this.allowMultipleSelection = true;   
                this.dispatcher = new Dispatcher();
            }

            public function handleSelection(event:PlaylistEvent):void
            {
                if (event.type == PlaylistEvent.ITEMS_SELECTED){
                    selectItem(event.index, event.count);
                    selectedIndex = event.index;
                }
            }

            private function selectItem(index:Number, count:Number):void
            {
                var tempArray:Array = this.dataProvider.toArray();
                var indiceVector:Vector.<int> = new Vector.<int>();
            var itemVector:Vector.<Object> = new Vector.<Object>();
            var n:int = 1;
            for (var i:int=0;i<tempArray.length;i++){
                var item:PlaylistItem = tempArray[i];
                if (i<index) deselectItem(item);
                else if (i<index+count) {
                    item.Selected=true;
                    indiceVector[count-n] = i;
                    itemVector[count-n++] = item;

                }
                else deselectItem(item);
            }
            selectedItems = itemVector;
            selectedIndices = indiceVector;
        }

        private function deselectItem(item:PlaylistItem):void
        {
            item.Selected = false;
        }


        public function nextSlide():void
        {
            if (dataProvider.length == 0) return;
            var index:int = selectedIndex;

            if (index < 0) {
                var event1:PlaylistEvent = new PlaylistEvent(PlaylistEvent.ITEMS_SELECTED, 0, 1);
                dispatcher.dispatchEvent(event1);
                return;
            }

            var item:PlaylistItem = PlaylistItem(dataProvider.getItemAt(index));
            if (index < dataProvider.length-1) {
                trace("going to index", index+1);
                var event:PlaylistEvent = new PlaylistEvent(PlaylistEvent.ITEMS_SELECTED, index+1, 1);
                dispatcher.dispatchEvent(event);
                return;
            }
        }

        public function previousSlide():void
        {
            if (dataProvider.length == 0) return;
            var index:int = selectedIndex;
            if (index < 0) {
                var event1:PlaylistEvent = new PlaylistEvent(PlaylistEvent.ITEMS_SELECTED, 0, 1);
                dispatcher.dispatchEvent(event1);
                return;
            }

            var item:PlaylistItem = PlaylistItem(dataProvider.getItemAt(index));
            if (index > 0) {
                var event:PlaylistEvent = new PlaylistEvent(PlaylistEvent.ITEMS_SELECTED, index-1, 1);
                dispatcher.dispatchEvent(event);
                return;
            }
        }
    }
}

这是我的队友EventMap

<?xml version="1.0" encoding="utf-8"?>
<mate:EventMap xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:mate="http://mate.asfusion.com/"
               xmlns:air="de.websector.mate.extensions.air.*">
    <fx:Script>
        <![CDATA[

            import components.Playlist;
            import events.PlaylistEvent;
            import mx.controls.Alert;
            import mx.events.*;
            import mx.logging.*;
            import mx.logging.Log;
            import spark.components.Application;

            private var logger:ILogger;     
            private function initLogger():void{
                logger = Log.getLogger("MainEventMap");
                trace("started init maineventmap");
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <mate:EventHandlers type="{FlexEvent.APPLICATION_COMPLETE}">
            <mate:InlineInvoker method="initLogger"/>
            <mate:ObjectBuilder generator="{MainManager}"/>
            <mate:EventAnnouncer type="{InitEvent.SYSTEM_INIT_COMPLETE}" />
        </mate:EventHandlers>
        <mate:Injectors target="{Playlist}">
            <mate:PropertyInjector targetKey="dataProvider" source="{MainManager}" sourceKey="playlistItems" />
        </mate:Injectors>
        <mate:Injectors target="{MainManager}">
            <mate:PropertyInjector targetKey="playlist" source="{Playlist}" />
        </mate:Injectors>

        <!-- Playlist events! __________________________________________________________________________________________________-->

        <mate:EventHandlers type="{PlaylistEvent.ITEMS_SELECTED}">
            <mate:MethodInvoker generator="{Playlist}" method="handleSelection" arguments="{event}" />
        </mate:EventHandlers>


    </fx:Declarations>

</mate:EventMap>

2 个答案:

答案 0 :(得分:1)

Mate无法通过对其事件总线的引用来填充您的私有调度程序变量,因此事件映射永远不会“听到”您在其上调度的事件,并且无法调用您的方法。

这似乎是一种在同一个组件上调用方法的愚蠢方式,我认为这表明你应该以某种方式分离逻辑。我建议你有一个包含dataprovider和selectedIndex的Presentation Model,并为它注入一个引用。然后,您的List将绑定到dataprovider和selectedIndex。 pm可以监听总线上的事件以维护selectedIndex,或者您可以直接从View组件中操作它。

这样,当你意识到拥有一个包含按钮的List子类时会有点问题,你可以附加一个包含List和按钮的新Group子类,没有任何问题。

有关如何正确注入事件总线的示例(方便地显示如何使用PM),请查看http://www.developria.com/2010/05/refactoring-with-mate.html。看起来您已经开始了解事件总线,但是您可能希望通过查看http://www.developria.com/2010/05/pass-the-eventdispatcher-pleas.html来获得更多细节。

答案 1 :(得分:0)

有人指出#Flex(irc://irc.freenode.net/flex),错误是使用MethodInvoker,因为它构造了一个新的类实例,然后运行handleSelection方法实例。我通过将播放列表实例注入EventMap并使用InlineInvoker来解决了这个问题。

<mate:EventHandlers type="{PlaylistEvent.ITEMS_SELECTED}">
        <mate:InlineInvoker method="{playlist.handleSelection}" arguments="{event}" />
    </mate:EventHandlers>

所以问题在于使用配合框架,而不是在Flex或数据绑定或事件调度中。