萬盛學電腦網

 萬盛學電腦網 >> 圖文處理 >> Flash教程 >> as制作可拖動單元格的菜單

as制作可拖動單元格的菜單

單元格是可以拖動的,並且可以在三個List之間拖動,不過沒加自動滾動來著。AS相當簡單,三個List組件分別是list1、list2、list3,然後把所有代碼copy到第一幀就可以了。
  import fl.events.ListEvent;
  import fl.controls.List;
  import fl.controls.listClasses.ICellRenderer;
  import fl.controls.listClasses.ListData;
  //為組件添加數據
  var i:uint = 0;
  for(i = 0;i<21;i++){
  list1.addItem({label:"label" + i});
  }
  for(i = 21;i<41;i++){
  list2.addItem({label:"label" + i});
  }
  for(i = 41;i<61;i++){
  list3.addItem({label:"label" + i});
  }
  //添加偵聽器
  list1.addEventListener(ListEvent.ITEM_ROLL_OVER,itemRollOverHandler);
  list2.addEventListener(ListEvent.ITEM_ROLL_OVER,itemRollOverHandler);
  list3.addEventListener(ListEvent.ITEM_ROLL_OVER,itemRollOverHandler);
  list1.addEventListener(ListEvent.ITEM_ROLL_OUT,itemRollOutHandler);
  list2.addEventListener(ListEvent.ITEM_ROLL_OUT,itemRollOutHandler);
  list3.addEventListener(ListEvent.ITEM_ROLL_OUT,itemRollOutHandler);
  stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
  stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
  //標記的組件和相應的行
  var lastCaredList: List = null;
  var lastCaredIndex:int = -1;
  var dragedList: List = null;
  var dragedIndex:int = -1;
  var itemValue:Object = null;
  //單元格渲染器
  var cellRendererClass:Class = List.getStyleDefinition()["cellRenderer"];
  var cellRenderer:ICellRenderer = new cellRendererClass() as ICellRenderer;
  //偵聽器
  function itemRollOverHandler(event: ListEvent):void{
  //標記組件和行
  lastCaredList = event.target as List;
  lastCaredIndex = event.index;
  }
  function itemRollOutHandler(event: ListEvent):void{
  //取消標記
  lastCaredList = null
  lastCaredIndex = -1;
  }
  function mouseDownHandler(event:MouseEvent):void{
  if(lastCaredList != null){
   //刪除標記的行
   itemValue = lastCaredList.removeItemAt(lastCaredIndex);
   createCellRenderer();
  }
  }
  function mouseUpHandler(event:MouseEvent):void{
  if(itemValue){
   removeChild(cellRenderer as Sprite);
   stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
   //判斷,是加到新組件中還是原組件中
   if(lastCaredList){
   lastCaredList.addItemAt(itemValue,lastCaredIndex);
   }else{
   dragedList.addItemAt(itemValue,dragedIndex);
   }
   itemValue = null;
  }
  }
  function mouseMoveHandler(event:MouseEvent):void{
  //拖動渲染器
  cellRenderer.x = mouseX;
  cellRenderer.y = mouseY;
  event.updateAfterEvent();
  }
  function createCellRenderer():void{
  dragedList = lastCaredList;
  dragedIndex = lastCaredIndex;
  //拖動的渲染器
  cellRenderer.listData = new ListData(itemValue.label,itemValue.icon,lastCaredList,0,0,0);
  cellRenderer.selected = true;
  cellRenderer.setSize(lastCaredList.width,lastCaredList.rowHeight);
  cellRenderer.x = mouseX;
  cellRenderer.y = mouseY;
  addChild(cellRenderer as Sprite);
  stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
  }
copyright © 萬盛學電腦網 all rights reserved