Android拖放图像:ACTION_DROP从未调用
Posted
技术标签:
【中文标题】Android拖放图像:ACTION_DROP从未调用【英文标题】:Android drag and drop image: ACTION_DROP never called 【发布时间】:2014-04-29 12:03:45 【问题描述】:我在这里按照教程进行操作:http://www.tutorialspoint.com/android/android_drag_and_drop.htm 我想让图像在屏幕上可拖动。我在我的 xml 文件中添加了 FrameLayout 并在其中添加了图像。问题是 DragEvent.ACTION_DROP 永远不会被调用。我确保在我的容器框架布局上实现 onDrag 并返回 true,但它仍然没有被调用。
//MainActivity.java
public class MainActivity extends Activity
ImageView ima;
private static final String IMAGEVIEW_TAG = "Android Logo";
private android.widget.FrameLayout.LayoutParams layoutParams;
String msg;
int x_cord = 0;
int y_cord = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout container = (FrameLayout) findViewById(R.id.container);
container.setOnDragListener(new OnDragListener()
@Override
public boolean onDrag(View arg0, DragEvent arg1)
return true;
);
ima = (ImageView) findViewById(R.id.iv_logo);
ima.bringToFront();
ima.setTag(IMAGEVIEW_TAG);
ima.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);
// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
return true;
);
// Create and set the drag event listener for the View
ima.setOnDragListener(new OnDragListener()
@Override
public boolean onDrag(View v, DragEvent event)
layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
switch (event.getAction())
case DragEvent.ACTION_DRAG_STARTED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
// layoutParams.leftMargin = x_cord;
// layoutParams.topMargin = y_cord;
Log.e("y cord", String.valueOf(y_cord));
// v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
// y_cord = (int) event.getY();
// layoutParams.leftMargin = x_cord;
Log.e("y cord action drag ended", String.valueOf(y_cord));
layoutParams.topMargin = y_cord;
// Log.e("y cord", String.valueOf(y_cord));
v.setLayoutParams(layoutParams);
// Do nothing
break;
case DragEvent.ACTION_DROP:
Log.d(msg, "ACTION_DROP event");
y_cord = (int) event.getY();
Log.e("y cord action drop", String.valueOf(y_cord));
// layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
// Do nothing
break;
default:
break;
return true;
);
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_
android:layout_
android:layout_weight="0.70"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_logo"
android:layout_
android:layout_
android:src="@drawable/butterfly" />
</FrameLayout>
【问题讨论】:
【参考方案1】:在容器的拖动侦听器上调用 ACTION_DROP,而不是在被拖动的图像上。
【讨论】:
以上是关于Android拖放图像:ACTION_DROP从未调用的主要内容,如果未能解决你的问题,请参考以下文章