Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Drag a View and change his Parent
    primarykey
    data
    text
    <p>I need a way to do a drag and drop between a child view to a parent view. So I will put a image for better explanation:</p> <p><img src="https://i.stack.imgur.com/mP5rJ.png" alt="enter image description here"></p> <p>Yellow represent a class that extends RelativeLayout. Black represent a class that extends ViewGroup White represents ImageViews Red represents a View</p> <p>So I need to drop White Blocks(ImageViews) on Yellow View, but I only drag over Black View, because ImageView has a BlackView like a father, so my solution for now is change father´s dragged view, but when I add a view on YellowView this cancel my drag event, so I need to continue drag when I change between Views. Note, there is a only way for WhiteView that is place on RedView, if Touch up and White view is far of RedView White block will back to BlackView.</p> <p>So, i using this code I get from web:</p> <pre><code> public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action &amp; MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: touchDown(event); break; case MotionEvent.ACTION_MOVE: touchMove(event); break; case MotionEvent.ACTION_UP: touchUp(event); break; } if (aViewIsDragged()) return true; return false; } private void touchDown(MotionEvent event) { initialX = (int)event.getRawX(); initialY = (int)event.getRawY(); lastTouchX = (int)event.getRawX() + (gridPageWidth); lastTouchY = (int)event.getRawY(); } public boolean onLongClick(View v) { if(positionForView(v) != -1) { movingView = true; dragged = positionForView(v); return true; } return false; } private void touchMove(MotionEvent event) { if (movingView &amp;&amp; aViewIsDragged()) { lastTouchX = (int) event.getX(); lastTouchY = (int) event.getY(); moveDraggedView(lastTouchX, lastTouchY); manageSwapPosition(lastTouchX, lastTouchY); manageEdgeCoordinates(lastTouchX); manageDeleteZoneHover(lastTouchX, lastTouchY); } } private void moveDraggedView(int x, int y) { ImageView child = null; View childAt = getChildAt(dragged); int width = childAt.getMeasuredWidth(); int height = childAt.getMeasuredHeight(); int left = x - (1 * width / 2); int t = y - (1 * height / 2); childAt.layout(left, t, left + width, t + height); //TODO at this point if left less than 0, I detect part of view is out and I need to change this view to Yello if (left &lt;0){ child = (ImageView) childAt; ViewGroup parent = (ViewGroup) getParent(); cloneimage = new ImageView(getContext()); cloneimage.setBackgroundDrawable(child.getDrawable()); parent.addView(cloneimage); } } </code></pre> <p>So I think it´s a little dificult, but If any one help me change a WhiteView from Black to Yellow and keep dragged I will apreciate.</p> <p>Thank´s!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload