Pankaj Kumar
Pankaj Kumar

Reputation: 83008

Design movable view : Android

How to design movable (Drag/Drop) view in android.

In my case enter image description here

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

Answers (1)

Ron
Ron

Reputation: 24235

Have a members declared for holding the currently dragging bitmap, view, and current dragging x and y coords.

  • Override the onTouch of the parent view.
  • In 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.
  • In ACTION_MOVE draw (invalidate) the dragging view bitmap at current x and y coordinates.
  • In 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

Related Questions