简体   繁体   English

使用拖放将ImageViews添加到相对布局

[英]adding ImageViews to Relative Layout using Drag and Drop

I am pretty new to Android and I am making an app with a Vertical Linear Layout at the bottom of the screen with a list of imageViews going across the screen. 我是Android的新手,正在制作一个屏幕底部具有垂直线性布局的应用程序,其中包含贯穿屏幕的imageViews列表。 Each of these are draggable onto the main part of the screen, however, if I drag one of the imageviews on the far right of the layout, it will drop the view far to the left of the actual drop point. 这些中的每一个都可以拖动到屏幕的主要部分上,但是,如果我将一个图像视图拖动到布局的最右边,它将把视图拖放到实际放置点的左侧。 What is strange is that if I try dragging the first imageview in the linearlayout (the one on the left side) it will land right under where I drop it. 奇怪的是,如果我尝试将第一个imageview拖动到linearlayout中(左侧的那个),它将降落在我放下的位置正下方。

My onDrag and onTouch Listeners... 我的onDrag和onTouch侦听器...

class MyDragListener implements OnDragListener {

     @Override
      public boolean onDrag(View v, DragEvent event) {

        float X = event.getX();
        float Y = event.getY();

        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:

          break;

        case DragEvent.ACTION_DRAG_ENTERED:

          break;
        case DragEvent.ACTION_DRAG_EXITED:        

          break;
        case DragEvent.ACTION_DROP:


          View view = (View) event.getLocalState();
          ViewGroup owner = (ViewGroup) view.getParent();
          owner.removeView(view);
          container = (RelativeLayout) v;
          container.setLayoutParams(findViewById(R.id.drawingbackground).getLayoutParams());

          container.addView(view);

          view.setX(X-(view.getWidth()/2));
          view.setY(Y-(view.getHeight()/2));
          view.setVisibility(View.VISIBLE);




          break;
        case DragEvent.ACTION_DRAG_ENDED:

          default:
          break;
        }
        return true;
      }
}

and the OnTouch... 和OnTouch ...

final class MyTouchListener implements OnTouchListener {
      public boolean onTouch(View view, MotionEvent motionEvent) {

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

          ClipData data = ClipData.newPlainText("", "");
          DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
          view.startDrag(data, shadowBuilder, view, 0);

          return true;

        }  if(motionEvent.getAction()== MotionEvent.ACTION_UP){
            view.performClick();
        }



        return false;

      }


}

Any assistance would be greatly appreciated! 任何帮助将不胜感激!

更改您的xml,使用相对布局而不是LinearLayout并修复其高度和宽度。

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

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