我有2个布局(比如说上,下).down布局包含一些图像网格,它们应该被拖到上面。在将单个图像从下向上拖动到其工作正常但在拖动2个或更多图像时,它们会分散。如何实现拖放从多个图像向下拖动,甚至重叠?
编辑:已添加代码
//drag code
img1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
id=img1.getId();
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(img1);
v.startDrag(dragData, myShadow, null, 0);
img1.setVisibility(View.INVISIBLE);
return true;
}
});
//其他图片的类似代码
//drop code
//linearLayout is the layout to which img1,img2 must be dragged and dropped
linearLayout.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
switch(id){
case (R.id.img1):
float X1 = event.getX();
float Y1 = event.getY();
ViewGroup owner1 = (ViewGroup) img1.getParent();
owner1.removeView(img1);
LinearLayout container1 = (LinearLayout)view;
container1.addView(img1);
img1.setX(X1 - (img1.getWidth() / 2));
img1.setY(Y1 - (img1.getHeight() / 2));
img1.setVisibility(View.VISIBLE);
break;
case R.id.img2:
float X2 = event.getX();
float Y2 = event.getY();
ViewGroup owner2 = (ViewGroup)img2.getParent();
owner2.removeView(img2);
LinearLayout container2 = (LinearLayout)view;
container2.addView(img2);
img2.setX(X2 - (img2.getWidth() / 2));
img2.setY(Y2 - (img2.getHeight() / 2));
img2.setVisibility(View.VISIBLE);
break;
}
case DragEvent.ACTION_DRAG_ENDED:
if (!event.getResult()) {
img1.setVisibility(View.VISIBLE);
}
if (!event.getResult()) {
img2.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
});
//其他图片的类似代码
答案 0 :(得分:0)
我认为问题在于LinearLayout,它在添加多个viewGroup时变得不稳定。我尝试过使用FrameLayout,它解决了我的问题。