//moveTree.mxml文件

  
    xmlns:comp="com.components.moveTree.*" xmlns:component="com.component.*">  
   
      import mx.events.DragEvent;  
   import mx.managers.DragManager;  
   import mx.core.DragSource;  
   import mx.events.DragEvent;  
   private function dropDrag(event:DragEvent):void {  
    tree.hideDropFeedback(event); //取消默认画线  
   }  
  ]]>  
 
  
   
   
   
     
   
   
   
   
     
    
   
  

    
     
      
      
   
  
     
      
      
   
  
     
      
      
   
  
    
  
  
 
 
 
  
 
 
  

 
  
 
    id="tree" labelField="@name" width="100%" 
  disclosureOpenIcon="@Embed(source='img/disclosure-open.png')"
  disclosureClosedIcon="@Embed(source='img/disclosure-closed.png')"
      height="100%" dragEnabled="true" dropEnabled="true" 
      dragMoveEnabled="true" dragDrop="dropDrag(event);" dataProvider="{xmlColl}" dropShadowVisible="true" borderVisible="false" color="#010101"/> 
 

 
 
 
 

 

//moveTree.as

package com.component
{
 import mx.collections.ICollectionView;  
 import mx.collections.IViewCursor;  
 import mx.controls.Tree;  
 import mx.core.mx_internal;  
 import mx.events.DragEvent;  
 
 import mx.managers.DragManager;  
  use namespace mx_internal; //命名空间  
 
 public class moveTree extends Tree {  
  
  public function moveTree() {  
   super();  
  }  
  /** 
   *  @private 
   *  Returns the stack of parents from a child item. 
   */ 
  private function getParentStack(item:Object):Array {  
   var stack:Array = [];  
   if (item == null)  
    return stack;  
   
   var parent:* = getParentItem(item);  
   while (parent) {  
    stack.push(parent);  
    parent = getParentItem(parent);  
   }  
   return stack;  
  }  
  override protected function dragDropHandler(event:DragEvent):void {  
   
   if (event.dragSource.hasFormat("treeItems")) {  
    var items:Array = event.dragSource.dataForFormat("treeItems") as Array;  
    var i:int;  
    var n:int;  
    
    if (event.action == DragManager.MOVE && dragMoveEnabled) {  
     if (event.dragInitiator == this) {  
       calculateDropIndex(event);  
    
      var index:int;  
      var parent:*;  
      var parentItem:*;  
      
      var dropIndex:int = _dropData.index;  
      
      // 获得 拖动目标 项的父节点  。  
      var dropParentStack:Array = getParentStack(_dropData.parent);  
      dropParentStack.unshift(_dropData.parent); //将父节点添加到数组的开头  
      n = items.length;  
      for (i = 0;i < n;i++) {  
       parent = getParentItem(items[i]); //获得父节点  
       index = getChildIndexInParent(parent,items[i]); //获得父节点的索引号  
       
       var item:* = items[i];  
       var itemName:String = item.localName().toString(); //  
       var targetParent:*;  
       if(itemName == "rootdata"){
    
       removeChildItem(parent,items[i],index);  

       if (parent == _dropData.parent && index < _dropData.index)  
        dropIndex--;  
       
       addChildItem(targetParent,items[i],dropIndex);  
                  
             }
       for each (parentItem in dropParentStack) {  
    
        // 不能把节点放到自身的子节点下面  
        if (items[i] === parentItem)  
         return;  
        
        if (parentItem) {  
         var targetParentName:String = parentItem.localName().toString();  
         if (itemName == "data" && targetParentName == "subdata") {  
          targetParent = parentItem;  
          break;  
         }  
         if (itemName == "subdata" && targetParentName == "rootdata") {  
          targetParent = parentItem;  
          break;  
         }  
        }  
        
       }  
       
       if (targetParent) { 
        removeChildItem(parent,items[i],index);  
    
        if (parent == _dropData.parent && index < _dropData.index)  
         dropIndex--;  
        
        addChildItem(targetParent,items[i],dropIndex);  
       }  
      }  
      
      return;  
     }  
    }  
    
 
    lastDragEvent = null;  
   }  
  }  
  /** 
   *  @private 
   *  Finds the index distance between a parent and child 
   */ 
  private function getChildIndexInParent(parent:Object,child:Object):int {  
   var index:int = 0;  
   if (!parent) {  
    var cursor:IViewCursor = ICollectionView(iterator.view).createCursor();  
    while (!cursor.afterLast) {  
     if (child === cursor.current)  
      break;  
     index++;  
     cursor.moveNext();  
    }  
   } else {  
    if (parent != null && _dataDescriptor.isBranch(parent,iterator.view) && _dataDescriptor.hasChildren(parent,  
     iterator.view)) {  
     var children:ICollectionView = getChildren(parent,iterator.view);  
     if (children.contains(child)) {  
      cursor = children.createCursor();  
      while (!cursor.afterLast) {  
       if (child === cursor.current)  
        break;  
       cursor.moveNext();  
       index++;  
      }  
      
     } else {  
        
     }  
    }  
   }  
   return index;  
  }  
 }