对于初学者来说,拖动并将拖动侦听器设置在同一视图上,通常拖动侦听器是另一个视图,而不是被移动的视图,以便在其上方、进入或退出等情况下监听被拖动视图的移动。
我认为你应该这样做:
编辑2:
//TODO change the view that listens to the drag event to root view layout
RelativeLayout layout = findViewById(R.id.yourRootViewLayout) //Change this to your root layout
layout.setOnDragListener(new View.OnDragListener() {
//I changed the view that listens to the dragging,
//not sure if that's the one you prefer but you can choose your own
//to listen to these events if it overlaps or not, etc.
@Override
public boolean onDrag(View v, DragEvent event) {
Log.d("Vishal check", String.valueOf(event.getAction()));
switch(event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_STARTED");
return true;
// Do nothing
case DragEvent.ACTION_DRAG_ENTERED:
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
return true;
case DragEvent.ACTION_DRAG_EXITED :
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_EXITED");
// view.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
return true;
//I put the code that you had in exited in ended because that's what
//listens to when the drag stops no matter where it is
case DragEvent.ACTION_DRAG_ENDED :
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_ENDED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
//TODO change the code below
View newView = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) newView.getParent();
owner.removeView(newView);
RelativeLayout container = (RelativeLayout) v;
container.addView(newView);
newView.setVisibility(View.VISIBLE);
newView.setX(x_cord);
newView.setY(y_cord);
return true;
case DragEvent.ACTION_DROP:
Log.d("Vishal Drag", "ACTION_DROP event");
/* x_cord = (int) event.getX();
y_cord = (int) event.getY();
View view = (View) event.getLocalState();
view.setX(x_cord - (view.getWidth() / 2));
view.setY(y_cord - (view.getWidth() / 2));
view.setVisibility(View.VISIBLE);*/
return true;
// Do nothing
default:
return true;
}
//return true;
}
});
addedTxtView.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);
//I changed addedTxtView to the current view being clicked
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(v);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.d("Vishal sdk","in if");
//TODO Edit added v instead of null
v.startDragAndDrop(dragData, myShadow, v, 0);
} else {
Log.d("Vishal sdk","in else");
//TODO Edit added v instead of null
v.startDrag(dragData, myShadow, v, 0);
}
//Let addedTxtView be invisible so there aren't two views at the same time
addedTxtView.setVisibility(View.INVISIBLE);
return false;
}
});
我还编辑了上面的部分:如果拖动事件ACTION\u Drag\u结束,我让addedTextView在拖动结束后再次可见。