简体   繁体   English

Android将ImageView拖放到另一个ImageView(或布局)中

[英]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. 我想在另一个ImageView或Layout中完成一个简单的ImageView拖放操作。 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. 我知道这样做的唯一方法是通过2个线性布局,一个“FROM”和“TO”,并为第二个布局指定灰色字母的背景图像。

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. 我已经设法让onTouch和onDrag工作并检测移动时,但我想把它锁定到位,这样它就不能被拖回原来的地方了。

Great thanks! 十分感谢!

If it should stay in the bottom_container, just unregister the listeners, if it has been dropped there, eg like this: 如果它应该保留在bottom_container中,只需取消注册侦听器,如果它已被删除,例如:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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