Flex中如何使用addChild()和removeChild()函数动态添加或删除Accordion容器中项目的例子

By Minidxer | October 5, 2008

接下来的例子演示了Flex中如何使用addChild()和removeChild()函数,动态添加或删除Accordion容器中项目。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white">
  6.  
  7.     <mx:Script>
  8.         <![CDATA[
  9.             import mx.containers.VBox;
  10.  
  11.             private const MAX_CHILDREN:uint = 5;
  12.  
  13.             private function accordion_addChild():void {
  14.                 if (accordion.numChildren < MAX_CHILDREN) {
  15.                     var vbox:VBox = new VBox();
  16.                     vbox.label = "child " + accordion.numChildren;
  17.                     vbox.percentWidth = 100;
  18.                     vbox.percentHeight = 100;
  19.                     var randColor:uint = Math.random() * 0xFFFFFF;
  20.                     vbox.setStyle("backgroundColor", randColor);
  21.                     accordion.addChild(vbox);
  22.                 }
  23.             }
  24.  
  25.             private function accordion_deleteChild():void {
  26.                 if (accordion.selectedChild) {
  27.                     accordion.removeChild(accordion.selectedChild);
  28.                 }
  29.             }
  30.         ]]>
  31.     </mx:Script>
  32.  
  33.     <mx:ApplicationControlBar dock="true">
  34.         <mx:Button label="Add child"
  35.             click="accordion_addChild();" />
  36.         <mx:Button label="Delete child"
  37.             click="accordion_deleteChild();" />
  38.     </mx:ApplicationControlBar>
  39.  
  40.     <mx:Accordion id="accordion" width="240" height="160" />
  41.  
  42. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Accordion | 2 Comments » | Tags: , ,

你可能还对下列文章感兴趣:

2 comments | Add One

  1. wuw - 10/8/2011 at 3:44 pm

    有没有动态加载视频的例子?

  2. narimushroom - 01/17/2012 at 3:33 pm

    在删除时好像需要强制类型转换一下
    accordion.removeChild((DisplayObject)(accordion.selectedChild));

Leave a Comment

Name(*):

E-Mail(*) :

Website :

Comments :

Search Posts