代码之家  ›  专栏  ›  技术社区  ›  Neil

Flex4列表项呈现器子项和事件

  •  0
  • Neil  · 技术社区  · 14 年前

    我有一个自定义项目渲染器,我在列表中使用。项目呈现器中有一个复选框和一个颜色选择器。我已经为这两个项创建了自己的自定义事件类,它们不会冒泡它们的事件。

    您也可以单击列表中的项,我有3个侦听器附加到列表,我不希望列表项处理程序在单击列表的子项时触发,我如何做到这一点?

    摘录如下:

         protected function updateList():void
      {
       var proxy:ApplicationDataProxy = ApplicationDataProxy(facade.retrieveProxy(ApplicationDataProxy.NAME));
       list.addEventListener(CustomColorEvent.UPDATED_COLOR, colorClickHandler);
       list.addEventListener(CustomMenuEvent.CHECK_CLICKED, checkClickHandler);
       list.addEventListener(MouseEvent.CLICK, clickHandler);
       list.itemRenderer = new ClassFactory(FlightItemRenderer);
       list.dataProvider = proxy.flightsList;
      }
    
      protected function colorClickHandler(event:CustomColorEvent):void
      {
       sendNotification(ApplicationFacade.UPDATE_COLOR, {id:event.data, color:event.color});
      }
    
      protected function checkClickHandler(event:CustomMenuEvent):void
      {
       sendNotification(ApplicationFacade.SHOW_FLIGHT, {id:event.data, visible:event.visible});
      }
    
      protected function clickHandler(event:Event):void
      {
       // also gets fired from colours and checkbox, BUT I DON'T WANT IT TO!!!
      }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Greg    14 年前

    将单击侦听器添加到项目呈现器中,并检查event.target属性以查看是否是单击的复选框,如果是,则可以调用 event.stopImmediatePropagation() . 下面是一个非常简单的示例,MouseEvent.CLICK的其他侦听器都不会被激发。

    <s:List>
        <s:itemRenderer>
            <fx:Component>
                <s:CheckBox click="checkbox1_clickHandler(event)" />
                    <fx:Script>
                        <![CDATA[
                            protected function checkbox1_clickHandler(event:MouseEvent):void
                            {
                                //You could also add this click listener
                                //to the renderer itself if you need to do
                                //something when the checkbox is clicked
                                event.stopImmediatePropagation();
                            }
                        ]]>
                    </fx:Script>
            </fx:Component>
        </s:itemRenderer>
    </s:List>