Question

it seems that there are a lot of solutions of how to use dragging of views on android .

however, they are either too slow or buggy.

a known solution is to have a framelayout , and the view simply changes its layoutParams as it moves using the OnTouchListener ,as described here: http://www.anddev.org/novice-tutorials-f8/android-drag-and-drop-of-normal-views-not-bitmaps-t13404.html however, if the view is an imageView , when reaching the most right (or most bottom) side of the framelayout , the image shrinks .

another possible solution is to use the canvas in order to show the dragged content , as described here: http://www.anddev.org/basic_drag_and_drop-t3095.html however, i'm not sure how to use it for complex views.

does anyone know of a good solution , one that let the size of the dragged view stay the same , no matter where it is in its container?

please help me .

Was it helpful?

Solution

Here's a complete solution for a simple imageView dragging:

findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener()
  {
    int prevX,prevY;

    @Override
    public boolean onTouch(final View v,final MotionEvent event)
      {
      final FrameLayout.LayoutParams par=(FrameLayout.LayoutParams)v.getLayoutParams();
      switch(event.getAction())
        {
        case MotionEvent.ACTION_MOVE:
          {
          par.topMargin+=(int)event.getRawY()-prevY;
          prevY=(int)event.getRawY();
          par.leftMargin+=(int)event.getRawX()-prevX;
          prevX=(int)event.getRawX();
          v.setLayoutParams(par);
          return true;
          }
        case MotionEvent.ACTION_UP:
          {
          par.topMargin+=(int)event.getRawY()-prevY;
          par.leftMargin+=(int)event.getRawX()-prevX;
          v.setLayoutParams(par);
          return true;
          }
        case MotionEvent.ACTION_DOWN:
          {
          prevX=(int)event.getRawX();
          prevY=(int)event.getRawY();
          par.bottomMargin=-2*v.getHeight();
          par.rightMargin=-2*v.getWidth();
          v.setLayoutParams(par);
          return true;
          }
        }
      return false;
      }
  });

OTHER TIPS

I am using drag and drop in a current project, where the various Views are placed within a RelativeLayout according to LayoutParams applied to them. Like you, I found that Views would 'shrink' when reaching the right or bottom of the ViewGroup container. It's probably quite obvious that this will happen, really, considering that during the measure and layout phases, the system is going to determine that a given View is suddenly going to have to have much smaller dimensions than you actually wish if it's still going to be able to fit into the parent when placed near the extremes.

A very simple solution I've done to get around that at the moment is to simply oversize my ViewGroup relative to the screen size.

Furthermore, you may decide that you don't actually want Views to appear partially off-screen, in which case you'd use some program logic to prevent the View objects' margins being set such that the Views are allowed to go off the parent container's boundary.

Another solution might be to override onMeasure() or similar to force the child View to force a larger size than the parent deems is available to it -- if that's possible -- to force the View to be placed there in the desired size, overlapping the edge. That's just an idea off the top of my head though and have not investigated it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top