Android / Java - Add textView to Drag-Drop GridView Tutorial / Example

admin

Administrator
Staff member
I've create a project based on a few classes from the following tutorial:

<a href="http://blahti.wordpress.com/2012/03/03/improved-drag-drop-for-gridview/" rel="nofollow">http://blahti.wordpress.com/2012/03/03/improved-drag-drop-for-gridview/</a>

Now I'm attempting to modify the example to be able to drag and drop not only an image - but an image and a textView at the same time.

How can this be accomplished?

I've instantiated the textView already:

Code:
tx = (TextView) findViewById(R.id.textView1);

Now I believe I'll need to modify the tutorial's acceptDrop method (in it's DropTarget class) but I'm not 100% sure how to do so.

The bottom line:

I simply need a bit of figuring out how to add a textView to this simple drag and drop gridView tutorial.

Full source can be seen and downloaded here

<a href="https://docs.google.com/file/d/0B0wYSnCBkoR6MmdWbnktYUpTc2FFakdVU3NYeUxDZw/edit" rel="nofollow">https://docs.google.com/file/d/0B0wYSnCBkoR6MmdWbnktYUpTc2FFakdVU3NYeUxDZw/edit</a>

P.S.

I left a comment regarding doing this in the tutorial's comment section and the author mentioned the following:

"You will need to come up with a custom class to define the items that go in the gridview. That view would display the text and an image. Have the new class implement the drag-drop interfaces. In the acceptDrop method copy both the text and the image.

Part of the problem you face is similar to having a custom list item with a list view. It might be good to find a few examples of that before you take on the drag-drop part."

I simply need a bit of help doing so...

<h2>DropTarget.java:</h2>

Code:
/**
 * Interface defining an object that reacts to objects being dragged over and dropped onto it.
 *
 */
public interface DropTarget {

    /**
     * Handle an object being dropped on the DropTarget
     * 
     * @param source DragSource where the drag started
     * @param x X coordinate of the drop location
     * @param y Y coordinate of the drop location
     * @param xOffset Horizontal offset with the object being dragged where the original
     *          touch happened
     * @param yOffset Vertical offset with the object being dragged where the original
     *          touch happened
     * @param dragView The DragView that's being dragged around on screen.
     * @param dragInfo Data associated with the object being dragged
     * 
     */
    void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo);

    /**
     * React to something started to be dragged.
     */    
    void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo);

    /**
     * React to something being dragged over the drop target.
     */    
    void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo);

    /**
     * React to a drag 
     */    
    void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo);

    /**
     * Check if a drop action can occur at, or near, the requested location.
     * This may be called repeatedly during a drag, so any calls should return
     * quickly.
     * 
     * @param source DragSource where the drag started
     * @param x X coordinate of the drop location
     * @param y Y coordinate of the drop location
     * @param xOffset Horizontal offset with the object being dragged where the
     *            original touch happened
     * @param yOffset Vertical offset with the object being dragged where the
     *            original touch happened
     * @param dragView The DragView that's being dragged around on screen.
     * @param dragInfo Data associated with the object being dragged
     * @return True if the drop will be accepted, false otherwise.
     */
    boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo);

    /**
     * Estimate the surface area where this object would land if dropped at the
     * given location.
     * 
     * @param source DragSource where the drag started
     * @param x X coordinate of the drop location
     * @param y Y coordinate of the drop location
     * @param xOffset Horizontal offset with the object being dragged where the
     *            original touch happened
     * @param yOffset Vertical offset with the object being dragged where the
     *            original touch happened
     * @param dragView The DragView that's being dragged around on screen.
     * @param dragInfo Data associated with the object being dragged
     * @param recycle {@link Rect} object to be possibly recycled.
     * @return Estimated area that would be occupied if object was dropped at
     *         the given location. Should return null if no estimate is found,
     *         or if this target doesn't provide estimations.
     */
    Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo, Rect recycle);

    // These methods are implemented in Views
    void getHitRect(Rect outRect);
    void getLocationOnScreen(int[] loc);
    int getLeft();
    int getTop();
}