我遇到了以下問題: 我想爲LinearLayout實現View.DragOnListener。當我長時間點擊屏幕時,OnDrag方法收到DragEvent.ACTION_DRAG_STARTED,但是當我釋放拖動在屏幕上時。此方法無法接收DragEvent.ACTION_DROP。 希望得到大家的幫助。在這裏我的代碼:OnDrag無法接收DragEvent.ACTION_DROP
package com.example.dragdroptestapp;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.widget.LinearLayout;
public class MyLinearLayout extends LinearLayout implements View.OnDragListener{
private final String TAG = "MyLinearLayout";
public MyLinearLayout(Context context) {
super(context);
initLongClick();
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initLongClick();
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initLongClick();
}
@Override
public boolean onDragEvent(DragEvent event) {
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.e(TAG, "Started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.e(TAG, "Entered");
break;
case DragEvent.ACTION_DROP:
Log.e(TAG, "Droped");
break;
default:
Log.e(TAG, "action event" + String.valueOf(action));
break;
}
return true;
}
@Override
public boolean onDrag(View v, DragEvent event) {
Log.e(TAG, "onDrag");
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.e(TAG, "Started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.e(TAG, "Entered");
break;
case DragEvent.ACTION_DROP:
Log.e(TAG, "Droped");
break;
default:
break;
}
return true;
}
private void initLongClick() {
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.e(TAG, "LongClick");
View.DragShadowBuilder myShadow = new DragShadowBuilder(v);
v.startDrag(null, myShadow, null, 0);
return true;
}
});
}
}