Frage

I would like to save the dragged button's text to swap it with other button's text - the one that I drop it onto.

So far I have this :

b.setOnDragListener( new OnDragListener(){

    Drawable defaultBackground = ((Button) b).getBackground();

    @Override
    public boolean onDrag(View v,  DragEvent event){
      String draggedWord = "";

      switch(event.getAction())                   
      {
         case DragEvent.ACTION_DRAG_STARTED:
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_STARTED");
            // ... what here ?
            // if I put draggedWord = ((Button) v).getText().toString();
            // it's triggered for every button with this listener
            Log.d(USER_SERVICE, "DraggedWord" + draggedWord);
            break;
         case DragEvent.ACTION_DRAG_ENTERED:
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_ENTERED");
            ((Button) v).setBackgroundColor(Color.GREEN);
            break;
         case DragEvent.ACTION_DRAG_EXITED :
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_EXITED");
            ((Button) v).setBackground(defaultBackground);
            break;
         case DragEvent.ACTION_DRAG_LOCATION  :
            break;
         case DragEvent.ACTION_DRAG_ENDED   :
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_ENDED");
            ((Button) v).setBackground(defaultBackground);
            break;
         case DragEvent.ACTION_DROP:
            Log.d(USER_SERVICE, "ACTION_DROP event");
            if( event.getResult() ){
              ((Button) v).setText(draggedWord);
            }
            break;
         default: 
           break;
         }
         return true;
    }
 });

event.getClipData() return null although I put there some data in my onLongClickListener

  b.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
       ClipData.Item item = new ClipData.Item( ((Button) v).getText() );

       String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
       ClipData dragData = new ClipData(
           ((Button) v).getText().toString(), 
           mimeTypes, 
           item);

       // Instantiates the drag shadow builder.
       View.DragShadowBuilder myShadow = new DragShadowBuilder(b);

       // Starts the drag
       v.startDrag(dragData,  // the data to be dragged
                   myShadow,  // the drag shadow builder
                   null,      // no need to use local data
                   0          // flags (not currently used, set to 0)
       );
       return true;
    }
 });

EDIT

That is what I get when I uncomment the block in ACTION_DRAG_STARTED section (this is a consequence of me dragging a button onto another):

  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord dach
  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord kot
  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord kubek
  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord samochód
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENTERED
  3177                   user  D  Action is DragEvent.ACTION_DRAG_EXITED
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENTERED
  3177                   user  D  ACTION_DROP event
  3177                   user  D  DragEvent: false
  3177           ViewRootImpl  I  Reporting drop result: true
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true
  1259          WindowManager  W  Drag is in progress but there is no drag window handle.
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true

EDIT2

Ok so I got to the point where I extract the desired string with the following:

 event.getClipDescription().toString();

but this gives me the following string:

DraggedWord ClipDescription { text/plain "drukarka" }

and getClipData returns null.

War es hilfreich?

Lösung

Ok I have managed to do that by

String currWord = event.getClipDescription().toString().split("\"")[1];
if( ! currWord.equals("")) 
  draggedWord = currWord;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top