【问题标题】:Drop event not detected between activities API 22+活动 API 22+ 之间未检测到丢弃事件
【发布时间】:2020-10-30 05:12:48
【问题描述】:

我想让用户能够在我的应用程序中从一个活动拖动到另一个活动,但未检测到放置事件

在第一个活动中开始拖动操作:

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

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

    Intent intent = new Intent();
    intent.putExtra("Phase", "Phase");    

    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);
    }


    startSecondActivity();

    finish();

    return true;
}

在第二个activity中接收drop操作

public boolean onDrag(View aView, DragEvent aEvent) {

    switch (aEvent.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:
                 This is not called on Android 22+
                 break;

          case DragEvent.ACTION_DROP:
          This is not called on Android 22+
                 break;
    }

    return true;
}

解决方案将获得50分奖励。

【问题讨论】:

标签: android drag-and-drop


【解决方案1】:

至于为什么不调用 onDrop,也许开发者网站上的这段文字解释了它。

用户在 View 的边界框内释放拖动阴影 可以接受数据的。系统发送 View 对象的监听器 动作类型为 ACTION_DROP 的拖动事件。拖动事件包含 在调用 startDrag() 时传递给系统的数据 开始了手术。侦听器应返回布尔值 true 如果接受丢弃的代码成功,则发送给系统。请注意,这 仅当用户将拖动阴影放在边界内时才会发生步骤 View 的框,其侦听器已注册以接收拖动事件。如果 用户在任何其他情况下释放拖动阴影,没有 发送 ACTION_DROP 拖动事件。

https://developer.android.com/guide/topics/ui/drag-drop

【讨论】:

    【解决方案2】:

    带有 GestureListener 的 onSwipeTouchListener 可能符合您的要求。您将 onTouch 事件转发给 GestureListener。 onSwipeTouchListener 附加到顶级视图。 OnSwipeTouchListener 获取对 Activity 的引用,当滑动时,OnSwipeTouchListener 会调用 Activity 以启动新的 Activity。

    有关代码,请参阅下面的答案。

    https://stackoverflow.com/a/48142661/13264143

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多