我已经根据以下教程中的几个类创建了一个项目:
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 中的项目。该 View 将显示文本和图像。让新类实现拖放接口(interface)。在acceptDrop方法中复制文本和图像。
您面临的部分问题类似于使用带有 ListView 的自定义列表项。在进行拖放部分之前,最好先找到一些示例。”
我只是需要一些帮助......
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();
}
最佳答案
在 GridView 中实现拖放的简单方法可以在此链接 http://www.androidviews.net/2012/12/pageddragdropgrid/ 上找到。
关于java - Android/Java - 将 textView 添加到拖放 GridView 教程/示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20963115/