Preparing empty TitleList for Drag/Drop

I discovered recently a little issue with the HorizontalList and TitleList components in Flex 2.   When they are empty and I drop an item on them, they will throw an error.    I tried to set assign the dataprovider property to “” or null, but this creates a “ghost” item in the list.   So I ended up using binding to create a blank object, assign that to the dataprovider and everything seems happy again.  I also read in the LiveDocs that  you could not do calculated for the TitleList and HorizontalList.  However, I seem to be able to accurately calculate the drop index, so not sure if this is still true in Flex 2 or I am just mistaken.   The code speaks for itself, here is my code snippet for calculating drop index and dropping and populating the List with an empty object:

import flash.events.Event;
import mx.events.ListEvent;
import mx.events.FlexEvent;
import mx.managers.DragManager;
import mx.events.DragEvent;
import mx.core.DragSource;
import mx.collections.IList;
import mx.controls.HorizontalList

[Bindable]
private var mediaList:Object;

/*Drag and drop events*/
public function doDragOver( event : DragEvent ) : void
{
     event.currentTarget.showDropFeedback( event );
     DragManager.showFeedback(DragManager.COPY);
}
public function doDragEnter( event : DragEvent ) : void
{
     var dragInitiator:HorizontalList = HorizontalList(event.currentTarget);
     DragManager.acceptDragDrop(dragInitiator);
}
public function doDragExit( event : DragEvent ) : void
{
     var dropTarget:HorizontalList = HorizontalList(event.currentTarget);
     dropTarget.hideDropFeedback(event);
}
public function doDragDrop( event : DragEvent ) : void
{
     var dropTarget:HorizontalList = HorizontalList(event.currentTarget);
     doDragExit( event );
     var items:Array = event.dragSource.dataForFormat("items") as Array;
     var dropLoc:int = dropTarget.calculateDropIndex(event);

     for(var i:uint=0; i < items.length; i++) {
          IList(dropTarget.dataProvider).addItemAt(items[i], dropLoc );
     }
}
public function doDragComplete( event : DragEvent ) : void
{
     doDragExit( event );
}

<mx:HorizontalList dataProvider="{mediaList}"
     styleName="slideShowBox"
     width="100%" height="120"
     dragMoveEnabled="true"
     dragEnabled="true"
     dragEnter="doDragEnter( event )"
     dragExit="doDragExit( event )"
     dragDrop="doDragDrop( event )"
     dragOver="doDragOver( event )"
     verticalAlign="middle"
     horizontalCenter="0"
    columnWidth="120"
    verticalCenter="0"
    horizontalScrollPolicy="auto"
    liveScrolling="true" />

2 Comments

Comments are closed.