【问题标题】:Android / Java - Add textView to Drag-Drop GridView Tutorial / ExampleAndroid / Java - 将 textView 添加到拖放 GridView 教程/示例
【发布时间】:2014-01-07 02:53:11
【问题描述】:

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

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

现在我正在尝试修改示例,使其不仅能够拖放图像,而且能够同时拖放图像和 textView。

如何做到这一点?

我已经实例化了 textView:

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

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

底线:

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

可以在这里查看和下载完整的源代码

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

附言

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

“您将需要提出一个自定义类来定义进入 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();
}

【问题讨论】:

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


    【解决方案1】:

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

    这个的 git 是https://github.com/mrKlar/PagedDragDropGrid

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多