清单 - >项呈示器(ComboBox)根据列表中的项目数捕获调度事件

时间:2014-07-24 15:45:45

标签: actionscript-3 list combobox

我在像这样的

这样的MXML组件中定义了List
<s:VGroup initialize="init();">

    <fx:Script>
        <![CDATA[
            private function init():void {
                stack.addEventListener(customEvent.CUSTOM_EVENT, listenerFunc)
            }
            private set dataProvider(dp:ArrayCollection): void {
                this.dp = dp
            }
    </fx:Script>

    <mx:Form>
        <mx:FormItem label="customLabel: "
            <s:List id="stack"
                    dataProvider="{dp}"
                    itemRenderer="comboBoxEditor"
        </mx:FormItem>
    </mx:Form>

我的itemRenderer是

<s:ItemRenderer>

    <fx:Script>
        <![CDATA[
            private function changeHandler(e:ListEvent):void {
                Alert.show(String(e));
                owner.dispatchEvent(new customEvent(customEvent.CUSTOME_EVENT, cb.selectedIndex));
            }
        ]]>
    </fx:Script>

    <mx:ComboBox id="cb"
                 dataProvider="{data}" 
                 change="changeHandler(event)" />

</s:ItemRenderer>

List中的dp是ArrayCollection的ArrayCollection,就像这个

一样
a = new ArrayCollection()
a.addItem({label: "a"})
a.addItem({label: "b"})
dp = new ArrayCollection()
dp.addItem(a)

每次我在组合框选择中进行更改时,都会创建一个事件并按预期进行相应的调度。但是所有者或父列表中的eventListener仅捕获与第一个项目相关的更改事件,即&#34; a&#34;在组合框?如果我选择b,则会创建一个事件,但不会被所有者列表捕获#34; stack&#34; .... 我做错了什么?

提前致谢

如果我再做一次     dp.addItem(a)中

然后在第二个组合框中,我为&#34; a&#34;触发了catch事件监听器事件。 abd&#34; b&#34;选择。第一个组合框也开始工作....

本质上基于我广泛的试运行,基于&#34; dp&#34;中的项目数量。和&#34; a&#34;,catch事件只会根据&#34; dp&#34;

中的项目数触发一定次数

1 个答案:

答案 0 :(得分:0)

我尝试重现你的问题,但对我来说一切正常。我真的不会在你的代码中看到错误。也许我的工作实例会帮助你。

Main.mxml

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:local="*" >
    <local:CustomComponent />
</s:Application>

CustomComponent.mxml

<?xml version="1.0"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" initialize="init()">
    <fx:Script><![CDATA[
        import mx.collections.ArrayCollection;

        private function init():void {
            stack.addEventListener(CustomEvent.EVENT, listenerFunc);
            var a:ArrayCollection = new ArrayCollection();
            a.addItem({label: "a"});
            a.addItem({label: "b"});
            var dp:ArrayCollection = new ArrayCollection();
            dp.addItem(a);
            stack.dataProvider = dp;
        }

        private function listenerFunc(event:CustomEvent):void {
            trace("blah - " + event.index);
        }
        ]]></fx:Script>

    <s:List id="stack" itemRenderer="comboBoxEditor" />
</s:VGroup>

CustomEvent.as

package {
    import flash.events.Event;

    public class CustomEvent extends Event {

        public static const EVENT:String = "customEvent";

        public var index:int;

        public function CustomEvent(type:String, index:int, bubbles:Boolean = false, cancelable:Boolean = false) {
            super(type, bubbles, cancelable);
            this.index = index;
        }
    }
}

comboBoxEditor.mxml

<?xml version="1.0"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
        import mx.events.ListEvent;


        private function changeHandler(e:ListEvent):void {
            owner.dispatchEvent(new CustomEvent(CustomEvent.EVENT, cb.selectedIndex));
        }
        ]]>
    </fx:Script>

    <mx:ComboBox id="cb"
                 dataProvider="{data}"
                 change="changeHandler(event)" />
</s:ItemRenderer>