简体   繁体   中英

Android Drag and Drop of ImageView into another ImageView (or layout)

I want to accomplish a simple Drag and Drop of an ImageView inside another ImageView or a Layout. Here's a picture of what I want to do

这就是我想要完成的

The only way I know to do this is through having 2 linear layouts, one "FROM" and "TO", and assign to the 2nd layout the background image of the grayed letter.

Here's my code

public class MainActivity extends Activity implements OnTouchListener,
    OnDragListener {

private static final String LOGCAT = null;

/*
 * Here are the links I've been using
 * http://www.techrepublic.com/blog/software-engineer/try-androids-useful-drag-and-drop-api/
 * http://codingjunkie.net/android-drag-and-drop-part-2/
 * http://javapapers.com/android/android-drag-and-drop/
 */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.letter_a).setOnTouchListener(this);        // My yellow letter a image
    findViewById(R.id.top_container).setOnDragListener(this);    // My 1st linear layout 
    findViewById(R.id.bottom_container).setOnDragListener(this); // My 2nd linear layout with "grayed a" image 
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    } else {
        return false;
    }
}

@Override
    public boolean onDrag(View layoutview, DragEvent dragevent) {

        int action = dragevent.getAction();
        View view = (View) dragevent.getLocalState();

        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
        break;
        case DragEvent.ACTION_DRAG_ENTERED:
        break;
        case DragEvent.ACTION_DRAG_EXITED:
        break;
        case DragEvent.ACTION_DROP:
          ViewGroup owner = (ViewGroup) view.getParent();
          owner.removeView(view);
          LinearLayout container = (LinearLayout) layoutview;
          container.addView(view);
          view.setVisibility(View.VISIBLE);
          break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.d(LOGCAT, "Drag ended");
            if (dropEventNotHandled(dragevent)){
                view.setVisibility(View.VISIBLE);
            }
          break;
        default:
          break;
        }
        return true;
    }

private boolean dropEventNotHandled(DragEvent dragEvent) {
    return !dragEvent.getResult();
}
}

I've managed to get the onTouch and onDrag working and detecting when moving, but I want to LOCK it in place so it can't be dragged back to it's original place.

Great thanks!

If it should stay in the bottom_container, just unregister the listeners, if it has been dropped there, eg like this:

case DragEvent.ACTION_DROP:
          ViewGroup owner = (ViewGroup) view.getParent();
          owner.removeView(view);
          LinearLayout container = (LinearLayout) layoutview;
          container.addView(view);
          view.setVisibility(View.VISIBLE);
          if(container.getId()==R.id.bottom_container){
              view.setOnTouchListener(null);
              owner.setOnDragListener(null);
          }
  break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM