Android / Java - 将textView添加到Drag-Drop GridView教程/示例中

时间:2014-01-07 02:53:12

标签: java android gridview drag-and-drop android-launcher

我基于以下教程中的几个类创建了一个项目:

http://blahti.wordpress.com/2012/03/03/improved-drag-drop-for-gridview/

现在我正在尝试修改示例,以便不仅可以拖放图像,还可以拖放图像和textView。

如何实现这一目标?

我已经实例化了textView:

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

现在我相信我需要修改教程的acceptDrop方法(在它的DropTarget类中),但我不是100%确定如何这样做。

底线:

我只需要弄清楚如何在这个简单的拖放gridView教程中添加textView。

可在此处查看并下载完整资源

https://docs.google.com/file/d/0B0wYSnCBkoR6MmdWbnktYUpTc2FFakdVU3NYeUxDZw/edit

P.S。

我在教程的评论部分留下了关于这样做的评论,作者提到了以下内容:

“您需要提供一个自定义类来定义gridview中的项目。该视图将显示文本和图像。让新类实现拖放接口。在acceptDrop方法副本中文字和图片。

您遇到的部分问题类似于具有列表视图的自定义列表项。在你采用拖放部分之前找到一些例子可能会很好。“

我只是需要一点帮助......

DropTarget.java:

/**
 * 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();
}

1 个答案:

答案 0 :(得分:0)

可以在此链接上找到在网格视图中实现拖放的简单方法http://www.androidviews.net/2012/12/pageddragdropgrid/

并且git就是https://github.com/mrKlar/PagedDragDropGrid