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

I want to do a simple drag and drop of an ImageView inside another ImageView or layout. Here is a picture of what I want to do

This is what I want to accomplish

The only way I know this is to have 2 linear layouts, one β€œOT” and β€œTO”, and assign a background image of a gray letter to the second layout.

Here is 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 managed to get onTouch and onDrag to work and detect when you move, but I want to WRITE it in place so that it cannot be dragged back to its original place.

Many thanks!

+4
source share
1 answer

, , , . :

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;
+2

Source: https://habr.com/ru/post/1523594/


All Articles