首页 > 解决方案 > 拖放在某些设备上的 2 个活动之间不起作用

问题描述

我使用以下代码将视图从一个活动拖到我的应用程序中的另一个活动。知道拖动开始时第二个活动(接收放置事件)没有创建/活动。

它适用于

三星 Note 3 Android 5 API 21,三星 Note 4 Android 6.0.1 API 23

但不工作

华硕 ZenPad 8.0 Android 5.1.1 API 22、Le Max 2 Android 6.0 API 23

感谢您的想法。

开始拖动操作:

public boolean onItemLongClick(AdapterView<?> aParent, View aView, int aPos, long aID) {

    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(aView);

    Intent intent = prepDNDIntent();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        aView.startDragAndDrop(ClipData.newIntent(DRAG_N_DROP_DESCRIPTION, intent), shadowBuilder, null, 0);
    } else {
        aView.startDrag(ClipData.newIntent(DRAG_N_DROP_DESCRIPTION, intent), shadowBuilder, null, 0);
    }


    startMainActivity();

    finishThisActivity();

    return true;
}

接收丢弃操作

public boolean validDragNDropOperation(View aView, DragEvent aEvent){
    boolean status = false;

    ClipDescription clipDescription = aEvent.getClipDescription();
    if (clipDescription != null && clipDescription.getLabel().toString().equalsIgnoreCase(DRAG_N_DROP_DESCRIPTION)) {
        status = true;
    }

    return status;
}


public boolean onDrag(View aView, DragEvent aEvent) {

    switch (aEvent.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:

               This is not called on some devices

               return validDragNDropOperation(aView, aEvent, false);

          case DragEvent.ACTION_DROP:
                 break;
    }

    return true;
}

知道我将两个活动都设置为 android:launchMode="standard" 和 "singleTask"但我以相同的结果结束。

编辑

在测试期间,我使用了一个 AlertDialog 来保存被拖动的视图(而不是使用活动),并且我遇到了同样的问题,当将视图从 AlertDialog 拖动到 Activity(拥有 AlertDialog)时,没有调用 ACTION_DRAG_STARTED。

标签: androiddrag-and-drop

解决方案


推荐阅读