我似乎无法理解Android的拖放功能。
我已经设置了4个单独的视图,并将onDragListener
设置为下面的代码。我希望用户将图像拖到四个视图中的一个上。当用户放下拖动的图像时,我想用相关数据填充放置它的视图。
我看到的问题是,所有四个观看次数都在ACTION_DRAG_ENDED
上与event.getResult() = true
做出回应。当拖动视图进入边界区域时,只有一个视图响应。根据Android文档(here) event.getResult()
返回false,除非发送了ACTION_DROP。但是所有四个视图都显示getResult()
为真,只有一个显示ACTION_DROP
(参见下面的Logcat)
private OnDragListener mydragListener = new OnDragListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
ClipData dragData;
boolean handled = false;
switch (action) {
case DragEvent.ACTION_DRAG_STARTED: {
dragSku = event.getClipDescription().getLabel().toString();
handled = true;
Log.d(TAG,"ACTION_DRAG_STARTED="+handled);
} break;
case DragEvent.ACTION_DRAG_ENDED: {
// Report the drop/no-drop result to the user
final boolean dropped = event.getResult();
if (dropped) {
Log.d(TAG,"THIS SHOULD BE DROPPED: "+v.toString());
handled = true;
}
else {
handled = false;
}
Log.d(TAG,"ACTION_DRAG_ENDED="+handled);
} break;
case DragEvent.ACTION_DROP:
// Gets the item containing the dragged data
ClipData.Item item = event.getClipData().getItemAt(0);
// Returns true. DragEvent.getResult() will return true.
handled = true;
Log.d(TAG,"ACTION_DROP="+handled);
break;
case DragEvent.ACTION_DRAG_EXITED: {
handled = false;
Log.d(TAG,"ACTION_DRAG_EXITED="+handled);
} break;
case DragEvent.ACTION_DRAG_ENTERED: {
handled = true;
Log.d(TAG,"ACTION_DRAG_ENTERED="+handled);
}
}
return handled;
}
};
12-10 12:13:56.240: D/BrowsePhonesDevices(25667): ClipData : ClipData { text/plain "sku340603" {T:sku} } : sku340603
12-10 12:13:56.275: D/BrowsePhonesDevices(25667): ACTION_DRAG_STARTED=true
12-10 12:13:56.275: D/BrowsePhonesDevices(25667): ACTION_DRAG_STARTED=true
12-10 12:13:56.275: D/BrowsePhonesDevices(25667): ACTION_DRAG_STARTED=true
12-10 12:13:56.275: D/BrowsePhonesDevices(25667): ACTION_DRAG_STARTED=true
12-10 12:13:56.960: D/BrowsePhonesDevices(25667): ACTION_DRAG_ENTERED=true
12-10 12:13:57.205: D/BrowsePhonesDevices(25667): ACTION_DROP=true
12-10 12:13:57.205: I/ViewRootImpl(25667): Reporting drop result: true
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): THIS SHOULD BE DROPPED: android.widget.ImageView@42dfa5f0
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): ACTION_DRAG_ENDED=true
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): THIS SHOULD BE DROPPED: android.widget.ImageView@42dee7b0
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): ACTION_DRAG_ENDED=true
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): THIS SHOULD BE DROPPED: android.widget.ImageView@42df7618
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): ACTION_DRAG_ENDED=true
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): THIS SHOULD BE DROPPED: android.widget.ImageView@42df8e48
12-10 12:13:57.215: D/BrowsePhonesDevices(25667): ACTION_DRAG_ENDED=true
myDragListener
应该做些什么,以便只有一个视图从event.getResult()
获得真实结果?
答案 0 :(得分:0)
在ACTION_DROP
。
您知道删除了什么内容(item
,请注意:您也可以使用getLocalState()
来传递Object
s),并知道它被丢弃的位置(onDrag
的第一个参数是dropTarget)。如果您想知道它来自哪里,您可以在调用view.startDrag(..., new View.DragShadowBuilder(view), view, 0)
时将视图作为本地状态传递。
ACTION_DRAG_STARTED
/ ACTION_DRAG_ENDED
表示:
嘿嘿,所有人,有人开始/完成了一个拖累&放下操作(带有
getResult()
结果),您现在可以显示/隐藏您接收放置的能力。
您可以在此处找到上述官方措辞:http://developer.android.com/guide/topics/ui/drag-drop.html#HandleEnd