我正在尝试为imageView onLongClick设置拖放功能。我注意到的问题是,对于DragEvent.ACTION_DRAG_EXITED
,事件中收到的X和Y线从最后DragEvent.ACTION_DRAG_LOCATION
坐标发生变化。这会导致可拖动的imageView在我删除后移动(或跳转)。这是我的实施。
首先,我在对象(imageView)上设置一个onLongClickListener,我想拖动它:
ivDrag.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),
mimeTypes, item);
DragShadowBuilder mShadow = new DragShadowBuilder(v);
v.startDrag(dragData, mShadow, v, 0);
dragView.setOnDragListener(dragListener);
return true;
}
});
然后我在我想要拖动的对象(imageView)上设置一个onDragListener:
ivDrag.setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
Logger.i(TAG, "Action is DragEvent.ACTION_DRAG_STARTED");
ivDrag.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENTERED:
containDragable = true;
Logger.i(TAG, "Action is DragEvent.ACTION_DRAG_ENTERED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Logger.i(TAG, "x_cord: " + x_cord + " //y_cord: " + y_cord);
break;
case DragEvent.ACTION_DRAG_EXITED:
containDragable = false;
Logger.i(TAG, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX(); // removing this line allows correct cords
y_cord = (int) event.getY(); // removing this line allows correct cords
Logger.d(TAG, "x_cord: " + x_cord + " //y_cord: " + y_cord);
// below re-positions the drag object
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION:
Logger.i(TAG, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Logger.i(TAG, "x_cord: " + x_cord + " //y_cord: " + y_cord);
break;
case DragEvent.ACTION_DRAG_ENDED :
Logger.i(TAG, "Action is DragEvent.ACTION_DRAG_ENDED");
// below is what makes sure the image does not disappear
ivDrag.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Logger.e(TAG, "inside runnable");
if (ivDrag.getVisibility() != View.VISIBLE) {
ivDrag.setVisibility(View.VISIBLE);
}
}
});
break;
case DragEvent.ACTION_DROP:
Logger.i(TAG, "ACTION_DROP event");
// Do nothing
break;
default:
break;
}
return true;
}
});
}
注意:如上所述,如果我从DragEvent.ACTION_DRAG_EXITED
中移除x,y绳索的更新,则日志会指示正确的坐标。但是,之后我无法重新拖动对象。
最后,我在将对象拖入容器时创建了一个监听器。
private View.OnDragListener dragListener = new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Logger.v(TAG, "drag started (onDrag)");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Logger.v(TAG, "drag entered (onDrag)");
break;
case DragEvent.ACTION_DRAG_EXITED:
Logger.v(TAG, "drag exited (onDrag)");
break;
case DragEvent.ACTION_DROP:
Logger.e(TAG, "drop (onDrag)");
// handle drop
break;
case DragEvent.ACTION_DRAG_ENDED:
Logger.v(TAG, "drag ended (onDrag)");
break;
default:
return false;
}
return true;
}
};
我错过了如何按预期工作?提前谢谢!