Reputation: 83008
How to design movable (Drag/Drop) view in android.
In my case
There will be four rows and all will be movable.
User can arrange all 4 rows like 1234, 2341, 3421, 1243...... any combination of 1234.
So how can it will be?
All rows are layouts.
Upvotes: 1
Views: 1088
Reputation: 24235
Have a members declared for holding the currently dragging bitmap, view, and current dragging x and y coords.
onTouch
of the parent view.ACTION_DOWN
event get the drawing cache(Bitmap
) of the dragging view by calling getDrawingCache()
and set to the drag bitmap member. Invalidate the layout (so that onDraw gets called, where you draw the currently dragging view). Hide the current dragging view.ACTION_MOVE
draw (invalidate) the dragging view bitmap at current x and y coordinates.ACTION_UP
destroy the dragging bitmap. calculate the current drop location and add the current dragging view to that position in the layout. UnHide the current dragging view. Reset the dragging members.In onDraw
of parent layout.
canvas.save();
canvas.drawBitmap(mDragBmp, mDragX, mDragY);
canvas.restore();
Upvotes: 1