Flex ItemRenderer 自定义组件

1.MyListItemRenderer.mxml:

 

<?xml version="1.0" encoding="utf-8"?>
<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"
    autoDrawBackground="true">
 
 <s:states>
  <s:State name="normal"/>
  <s:State name="hovered"/>
  <s:State name="selected" />
 </s:states>
 
 <s:layout>
  <s:VerticalLayout/>
 </s:layout>
 
 <s:HGroup verticalAlign="middle" paddingTop="0" paddingBottom="0">
  <mx:Image source="{data.photo}" width="50" height="40" alpha.hovered=".5"/>
  <s:Label text="{data.name}" color.hovered="0x1313cd" color.selected="0x000000" verticalAlign="bottom"/>
 </s:HGroup>
 
</s:ItemRenderer> 

2.ListItem.mxml:  //使用上面的ItemRenderer

 

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx"
      xmlns:local="zw.test.render.*" >
 <fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
 </fx:Declarations>
 
 <fx:Script> 
  <![CDATA[
   import spark.events.IndexChangeEvent;
   import zw.test.render.MyListItemRenderer;
   
   [Bindable]private var totalPrice:Number = 0.00;
   
   protected function itemIndexChangeHandler(event:IndexChangeEvent):void
   {
    if (ta.text.length!=0)
     ta.text=ta.text+"\n"+myList.selectedItem.name + " $"+myList.selectedItem.price;
    else ta.text=myList.selectedItem.name+ " $"+myList.selectedItem.price;
    totalPrice += Number(myList.selectedItem.price);
   }
   protected function buyBtn_clickHandler(event:MouseEvent):void
   {
    txtResponse.text="Thank you for your order totaling: " + usdFormatter.format(totalPrice) + "\nItems will ship in 3 days.";
   }
   
  ]]>
 </fx:Script>
 <fx:Declarations>
  <mx:CurrencyFormatter id="usdFormatter" precision="2" currencySymbol="$"
         decimalSeparatorFrom="." decimalSeparatorTo="." useNegativeSign="true"
         useThousandsSeparator="true" alignSymbol="left"/>
 </fx:Declarations>
 <fx:Style>
  @namespace "library://ns.adobe.com/flex/spark";
  #vGrp {
   color: #000000;
   fontFamily: "Arial";
   fontSize: "12";
  }
 </fx:Style>
 
 <s:Panel title="List Sample"
    width="100%" height="100%"  
    skinClass="skins.TDFPanelSkin">
  <s:VGroup id="vGrp" horizontalCenter="0" top="3"
      width="80%" height="75%">
   <s:HGroup verticalAlign="middle">
    <s:Label text="Select items to add, price will be shown when added:"
       verticalAlign="bottom"/>
   </s:HGroup>
   <s:HGroup >
    <s:List id="myList" height="157" width="160"  x="20" y="20"
      itemRenderer="zw.test.render.MyListItemRenderer"
      change="">
     <s:dataProvider>
      <s:ArrayCollection> 
       <local:Item name="Backpack" photo="assets/backpack.jpg" price="29.99"/>
       <local:Item name="Boots" photo="assets/boots.jpg" price="69.99"/>
       <local:Item name="Compass" photo="assets/compass.jpg" price="12.99"/>
       <local:Item name="Goggles" photo="assets/goggles.jpg" price="14.99"/>
       <local:Item name="Helmet" photo="assets/helmet.jpg" price="47.99"/>
      </s:ArrayCollection>
     </s:dataProvider>
    </s:List>
    <s:TextArea id="ta" width="{myList.width}" height="{myList.height}"
       color="0xAE0A0A" fontWeight="bold"/>
    <s:VGroup>
     <mx:Spacer height="10"/>
     <s:Label text="Total of Items selected: {usdFormatter.format(this.totalPrice)}"/>
     <s:Button id="buyBtn" horizontalCenter="0" bottom="30" label="Buy Now!"
         fontWeight="bold"
         click="buyBtn_clickHandler(event)"/>
     <s:Label id="txtResponse"/>
    </s:VGroup>
   </s:HGroup>
  </s:VGroup>
  <s:Label bottom="15" horizontalCenter="0" width="95%" verticalAlign="justify" color="#323232"
     text="The Spark List control displays a list of data items. Its functionality is
     very similar to that of the SELECT form element in HTML. The user can select one or more items from the list.
     The List control automatically displays horizontal and vertical scroll bar when the list items do not fit
     the size of the control."/>
 </s:Panel>
 
</s:Application>

3.Item.as:

 

package zw.test.render
{
 public class Item
 {
  public function Item()
  {
  }
  private var _name:String;
  private var _photo:String;
  private var _price:String;
  
  public function get name():String
  {
   return _name;
  }
  
  public function set name(name:String):void
  {
   _name = name;
  }
  
  public function get photo():String
  {
   return _photo;
  }
  
  public function set photo(photo:String):void
  {
   _photo = photo;
  }
  
  public function get price():String
  {
   return _price;
  }
  public function set price(price:String):void
  {
   _price = price;
  }
 }
}

 

4.

你可能感兴趣的:(xml,Flex,Adobe)