如何通过多个活动进行拖放?

时间:2013-05-13 06:04:41

标签: android drag-and-drop android-activity

我使用Service调用来创建浮动窗口,浮动窗口浮动在所有其他视图/活动上。此窗口有自己的Activity,与Dialog不同。

现在我想在此窗口中添加一个Drag& Drop操作,例如,如果您长时间单击浮动窗口中的ImageView,则可以将其拖放到另一个Activity(或基础)中活动)。我曾尝试使用OnLongClickListener来触发Drag事件,并添加了OnDragListener来捕获Drop事件。这是我到目前为止所拥有的:

public class MyFloatableActivity extends FloatActivity {
private ImageView mImg;

private MyDragEventListener mDragListen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.float_activity));

    // This imageView is for Drag&Drop test
    mImg = (ImageView)findViewById(R.id.drag_img));
    mImg.setOnLongClickListener(new ImageView.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            ClipData dragData = ClipData.newPlainText("dragtext", "dragtext");

            v.startDrag(dragData, new View.DragShadowBuilder(v), null, 0);
            return false;
        }

    });

    mImg.setOnDragListener(mDragListen);

    switchToFloat(); // Make this activity to float
}

MyDragEventListener类是:

public class MyDragEventListener implements View.OnDragListener {

private ClipData mDragData;

@Override
public boolean onDrag(View v, DragEvent event) {
    final int action = event.getAction();
    ImageView img;

    if(v instanceof ImageView) {
        img = (ImageView)v;
    } else{
        return false;
    }

    switch(action) {
        case DragEvent.ACTION_DRAG_STARTED:
            if(event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                Log.d("DDD", "Drag started!!!");
                return true;
            }else {
                return false;
            }
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.d("DDD", "Entered!!!");
        case DragEvent.ACTION_DRAG_LOCATION:
        case DragEvent.ACTION_DRAG_EXITED:
            return true;
        case DragEvent.ACTION_DRAG_ENDED:
        case DragEvent.ACTION_DROP:
            Log.d("DDD", "Action drop!!!");
            return true;
    }
    return true;
}

我实施OnDragListener的原因是在删除ACTION_DROP时在基本活动中监听ImageView事件。这允许我确定ImageView是否被放置在目标图像或布局上。这是我的基础活动:

public class DragAndDropDemo extends Activity {

private ImageView mImg;

private MyDragEventListener mDragListen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drag_and_drop);

    //findViewById(R.id.drag_layout).setOnDragListener(mDragListen);

    mImg = (ImageView)findViewById(R.id.dest_img);

    mImg.setOnDragListener(mDragListen);
}

OnDragListener中的DragAndDropDemo问题未被调用,因此我无法捕获基本Activity中的Drop事件。我已经看过很多Drag and Drop的例子,但从来没有找到正确的解决方案。我想知道是否可以向Android中的不同Activity投掷Drag& Drop事件。如果Android可以做到,它会是什么?

有人可以提供帮助吗?

1 个答案:

答案 0 :(得分:1)

我自己找到了解决方案。我将OnDragListener集成到MyFloatableActivity,并将意图发送到DragAndDropDemo活动,以便在发生丢弃事件时接收意图。

所以这是我的代码。

public class MyFloatableActivity extends FloatActivity {
...

@Override
protected void onCreate(Bundle savedInstanceState) {
...

    mImg.setOnDragListener(new View.OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    if (event.getClipDescription().hasMimeType(
                            ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                        return true;
                    } else {
                        return false;
                    }
                case DragEvent.ACTION_DRAG_ENTERED:
                case DragEvent.ACTION_DRAG_LOCATION:
                case DragEvent.ACTION_DRAG_EXITED:
                    return true;
                case DragEvent.ACTION_DRAG_ENDED:
                case DragEvent.ACTION_DROP:
                    Intent intent = new Intent();
                    intent.setAction("com.test.DragAndDrop");
                    intent.putExtra("Drop", 0);
                    sendBroadcast(intent);
                    return true;

            }
            return false;
        }

    });
...
}

并在DragAndDropDemo中,

public class DragAndDropDemo extends Activity {
...
@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.test.DragAndDrop");
    registerReceiver(mBR, filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mBR);
}

BroadcastReceiver mBR = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        int flag = intent.getIntExtra("Drop", 0);
        switch (flag) {
            case 0:
                mText.setText("dropped!");
                mImg.setImageResource(R.drawable.icon_argentina);
                break;
        }
    }

};
}