1

好吧,比方说,你有一个标签,你有一个VerticalPanel上面有很多标签。请参阅此代码:

// Make the widget draggable.
Label w = new Label("OutSide Label");
w.getElement().setDraggable(Element.DRAGGABLE_TRUE);

// Add a DragStartHandler.
w.addDragStartHandler(new DragStartHandler() {
  @Override
  public void onDragStart(DragStartEvent event) {
    // Required: set data for the event.
    event.setData("text", "Hello World");
    // Optional: show a copy of the widget under cursor.
    event.getDataTransfer().setDragImage(w.getElement(), 10, 10);
  }
});

VerticalPanel vp=nw VerticalPanel();
vp.add(new Label("l1");
vp.add(new Label("l2"); 
vp.add(new Label("l3"); 

vp.addDomHandler(new DragOverHandler() {
  public void onDragOver(DragOverEvent event) {
    vp.getElement().getStyle().setBackgroundColor("#ffa");
  }
}, com.google.gwt.event.dom.client.DragOverEvent.getType());

vp.addDomHandler(new DropHandler() {            
  @Override
  public void onDrop(com.google.gwt.event.dom.client.DropEvent event) {
    // TODO Auto-generated method stub
    event.preventDefault();
    // Get the data out of the event.
    String data = event.getData("text");
    //Window.alert(data);
    /// WE NEED TO FILL IN THE MISSING CODE HERE
    /// WE NEED TO FILL IN THE MISSING CODE HERE
  }
}, DropEvent.getType());

如何编写代码,以便当用户将外部标签拖入区域时VerticalPanel,如果外部标签在现有小部件之间拖动,它将插入到现有标签之间VerticalPanel,例如如果外部标签在“l1”之间拖动& "l2",vp会显示:
l1
OutSide Label
l2
l3

4

1 回答 1

3

尽管未经测试,但理论上应该可以使用以下内容:

p.addDomHandler(new DropHandler() {
  @Override
  public void onDrop(DropEvent event) {
    // Get the target event.
    EventTarget eventTarget = event.getNativeEvent().getEventTarget();
    // Safe check for casting to Element.
    if (Element.is(eventTarget)) {
      Element elementTarget = Element.as(eventTarget);
      // Loop through VerticalPanel's children to find our target.
      for (int i = 0; i < p.getWidgetCount(); ++i) {
        if (p.getWidget(i).getElement().isOrHasChild(elementTarget)) {
          // Insert a new Label with the DataTransfer's data,
          // and remove the old one.
          p.insert(new Label(event.getData("text")), i);
          oldLabelContainer.remove(oldLabel);
          break;
        }
      }
    }
  }
}, DropEvent.getType());

当然,如果你有很多标签,应该避免循环。

于 2013-04-29T09:51:28.350 回答