在我的Android程序中,我想在线性布局中将我的imageview从一个位置移动到另一个位置。图像视图被拖动,但是当我放置在某个位置时,它会返回到起始位置。我能做什么?谷歌搜索了很多,但没有得到解决方案。
我的OnTouch:
img.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(img);
img.startDrag(data, shadowBuilder, img, 0);
img.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
我的OnDrag:
linearLayout.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
Log.d(msg, "ACTION_DROP event");
break;
default:
break;
}
return true;
}
});