Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume you have a homescreen like application, at least a view with items that you want to drag and drop.</p> <p>Like Austyn Mahoney says in his comment you can refere to the android <strong>open source</strong> homescreen code. But since drag and drop is a easy to implement feature you can handle it on your own, too.</p> <p>You need your items to be stored in e.g. an <strong>array</strong>. Then go through the event types.</p> <p>On every <strong>ACTION_DOWN</strong> you need to check if an item was hit by the user. E.g. if we have circles as items we need to capture the current ACTION_DOWN coordinates so we can check for them to be within the item and obtain the ID of the item that the user is about to drag according to the ACTION_DOWN coordinates.</p> <p>On <strong>ACTION_MOVE</strong> you just need to pass the current x and y coordinates to the item to redraw it on the new position.</p> <p>Well this is just the drag and drop feature. Now you need to test for the item to be in a specific sector you call bin. So you define a rectangle on your screen. in ACTION_MOVE your test the item to be within this area, thats all.</p> <p>Just some example code for drag and drop:</p> <pre><code>public boolean onTouchEvent(MotionEvent event){ int itemID = 0; int X = (int)event.getX(); int Y = (int)event.getY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: for(Items item : dragable_item){ //center coords of the item int centerX = item.getX() + 25; int centerY = item.getY() + 25; //calculate the distance (radius) from touch to item //get item to drag if(distance &lt; 20){ itemId = item.getID(); break; } } break; case MotionEvent.ACTION_MOVE: if(itemID &gt; 0){ Items[itemID-1].setX(X-25); Items[itemID-1].setY(X-25); } break; } } invalidate(); return true; } </code></pre> <p>If you wand to dig more into the code have a look at <a href="http://www.anddev.org/basic_drag_and_drop-t3095.html" rel="nofollow">anddev.org - drag and drop.</a></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.
 

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