快速拖动后OnDragListener没有收到回调

时间:2018-03-12 02:59:33

标签: android drag-and-drop touch drag

首先,此问题可能看起来像this question的副本,看起来像是问题here,但在查看详细信息后,您会发现它完全不同。

设置

我正在尝试实现一个简单的拖放功能:当我将ImageView A拖到ImageView B时,它们将交换它们的背景颜色。

Effect Illustration

您可以下载此小测试项目here。下面描述的问题可以通过克隆并在手机上构建来重现。

如果您只想查看代码,请查看MainActivity

问题

如果你慢慢地执行拖放操作(例如每次拖放都需要0.5秒),它会完美地运行。

然而,如果你尝试以你的禁食速度拖放(例如,一秒钟4到5次,无论如何你的最快速度),很快就会发现拖放动作不再能够执行了。通过查看日志,我发现没有任何拖动回调被触发

03-12 10:41:53.782 (...) I/dragEvent: view.startDragAndDrop has been called
03-12 10:41:53.787 (...) I/dragEvent: ACTION_DRAG_STARTED received
03-12 10:41:53.787 (...) I/dragEvent: ACTION_DRAG_STARTED received
03-12 10:41:53.789 (...) I/dragEvent: ACTION_DRAG_ENTERED received
03-12 10:41:53.789 (...) I/dragEvent: ACTION_DRAG_LOCATION received
03-12 10:41:53.796 (...) I/dragEvent: ACTION_DRAG_LOCATION received
03-12 10:41:53.802 (...) I/dragEvent: ACTION_DROP received
03-12 10:41:53.815 (...) I/dragEvent: ACTION_DRAG_ENDED received
03-12 10:41:53.815 (...) I/dragEvent: ACTION_DRAG_ENDED received
03-12 10:41:54.014 (...) I/dragEvent: view.startDragAndDrop has been called //Since this call, no callbacks were received

此时,应用程序不会冻结。 OnTouchListener仍在接收回调 拖放冻结因为我有一个标志来指示拖放是否正在进行,并且由于未触发ACTION_DRAG_ENDED,因此该标记停留在true,因此不再执行拖放操作。

我真的不知道为什么,因为我记录了所有内容,每行代码都正常运行,我希望从Android框架接收回调,但它不是......

为什么我认为它应该是可解决的

实际上,这个小小的测试项目是我试图克隆this app中实现的拖放图像交换。我无法在他们的应用程序中重现这个问题...所以我认为我的代码中肯定存在问题;或解决它的其他替代方案。

2 个答案:

答案 0 :(得分:1)

查看以下代码段

private void setupTouchListeners () {
    View.OnTouchListener listener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                return true;
            }
            if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
                boolean beyondLeft = event.getX() <= 0;
                boolean beyondRight = event.getX() >= v.getWidth();
                boolean beyondTop = event.getY() <= 0;
                boolean beyondBottom = event.getY() >= v.getHeight();
                if (beyondLeft || beyondRight || beyondTop || beyondBottom) {
                    startDragAndDrop(v);
                    return true;
                } else {
                    // done changes here, simply changed flag to false and return false
                    isDragging=false;
                    return false;
                }
            }
            return false;
        }
    };
    ivGrid1.setOnTouchListener(listener);
    ivGrid2.setOnTouchListener(listener);
    ivGrid3.setOnTouchListener(listener);
    ivGrid4.setOnTouchListener(listener);
}

如果通过上面的改变,它的颜色逻辑问题不是这个问题的一部分你将能够以你想要的速度拖动。

快乐编码:)

答案 1 :(得分:1)

你的快速拖拽导致你没有处理的晃动,并且拖放工具没有通知你它没有正式结束拖拽。以下更新的MainActivity.java进行了以下更改:

  1. 许多处理非拖拽相关事物的代码被删除了。我这样做是为了简化这里的发布,没有其他原因。
  2. 介绍GestureDetectorCompat以检测何时发生甩尾。我没有做任何事情,只记录它发生了。您可能需要考虑代码中触摸的手势检测器工具。
  3. 检查了ACTION_UP事件,以便在未完成拖动时重置布局(isDragging == true)。
  4. 当拖动开始时,似乎flings有一个混乱的结局。以下代码可以帮助您解决问题。

    <强> MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        ImageView ivGrid1, ivGrid2, ivGrid3, ivGrid4;
        private GestureDetectorCompat mDetector;
    
        //Drag and Drop
        View dragOriginView;
        Drawable whiteDrawable;
        Drawable tempDrawableStorage;
        boolean isDragging = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mDetector = new GestureDetectorCompat(this, new MyGestureListener());
    
            ivGrid1 = findViewById(R.id.grid_1);
            ivGrid2 = findViewById(R.id.grid_2);
            ivGrid3 = findViewById(R.id.grid_3);
            ivGrid4 = findViewById(R.id.grid_4);
    
            setupTouchListeners();
            setupDragListeners();
    
            whiteDrawable = new ColorDrawable(ContextCompat.getColor(this, android.R.color.white));
        }
    
        private void setupTouchListeners() {
            View.OnTouchListener listener = new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getActionMasked();
    
                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                            return true;
    
                        case MotionEvent.ACTION_MOVE:
                            boolean beyondLeft = event.getX() <= 0;
                            boolean beyondRight = event.getX() >= v.getWidth();
                            boolean beyondTop = event.getY() <= 0;
                            boolean beyondBottom = event.getY() >= v.getHeight();
                            if (beyondLeft || beyondRight || beyondTop || beyondBottom) {
                                startDragAndDrop(v);
                                return true;
                            }
                            isDragging = false;
                            break;
    
                        case MotionEvent.ACTION_UP:
                            if (isDragging) {
                                dragOriginView.setBackground(tempDrawableStorage);
                                tempDrawableStorage = null;
                                v.setAlpha(1.0f);
                                isDragging = false;
                            }
                    }
                    return false;
                }
            };
            ivGrid1.setOnTouchListener(listener);
            ivGrid2.setOnTouchListener(listener);
            ivGrid3.setOnTouchListener(listener);
            ivGrid4.setOnTouchListener(listener);
        }
    
        private void startDragAndDrop(View view) {
            if (isDragging) return;
            isDragging = true;
            dragOriginView = view;
            view.setAlpha(0.5f);
            ClipData.Item item = new ClipData.Item((String) view.getTag());
            ClipData dragData = new ClipData((String) view.getTag(), new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},
                                             item);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                view.startDragAndDrop(dragData, new View.DragShadowBuilder(view), null, 0);
            } else {
                view.startDrag(dragData, new View.DragShadowBuilder(view), null, 0);
            }
            Log.i("dragEvent", "view.startDragAndDrop has been called");
            tempDrawableStorage = view.getBackground();
            view.setBackground(whiteDrawable);
        }
    
        private void setupDragListeners() {
            View.OnDragListener listener = new View.OnDragListener() {
                @Override
                public boolean onDrag(View v, DragEvent event) {
                    Log.i("dragEvent", "<<<<<<<<<<<<<<called");
                    switch (event.getAction()) {
                        case ACTION_DRAG_STARTED:
                            Log.i("dragEvent", "ACTION_DRAG_STARTED received");
                            return event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
                        case ACTION_DRAG_ENTERED:
                            Log.i("dragEvent", "ACTION_DRAG_ENTERED received");
                            v.setAlpha(0.5f);
                            return true;
                        case ACTION_DRAG_LOCATION:
                            Log.i("dragEvent", "ACTION_DRAG_LOCATION received");
                            return true;
                        case ACTION_DRAG_EXITED:
                            Log.i("dragEvent", "ACTION_DRAG_EXITED received");
                            v.setAlpha(1f);
                            return true;
                        case ACTION_DROP:
                            Log.i("dragEvent", "ACTION_DROP received");
                            v.setAlpha(1f);
                            swapColor(v);
                            return true;
                        case ACTION_DRAG_ENDED:
                            Log.i("dragEvent", "ACTION_DRAG_ENDED received");
                            if (!event.getResult() && v == dragOriginView) {
                                dragOriginView.setBackground(tempDrawableStorage);
                                tempDrawableStorage = null;
                            }
                            v.setAlpha(1f);
                            isDragging = false;
                        default:
                            //Do nothing
                    }
                    return false;
                }
            };
            ivGrid1.setOnDragListener(listener);
            ivGrid2.setOnDragListener(listener);
            ivGrid3.setOnDragListener(listener);
            ivGrid4.setOnDragListener(listener);
        }
    
        private void swapColor(View dragTarget) {
            if (dragOriginView == dragTarget) {
                //Restore color
                dragOriginView.setBackground(tempDrawableStorage);
                tempDrawableStorage = null;
                return;
            }
            Drawable targetBackground = dragTarget.getBackground();
            dragOriginView.setBackground(targetBackground);
            dragTarget.setBackground(tempDrawableStorage);
            tempDrawableStorage = null;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            this.mDetector.onTouchEvent(event);
            return super.onTouchEvent(event);
        }
    
        class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
            private static final String DEBUG_TAG = "Gestures";
    
            @Override
            public boolean onDown(MotionEvent event) {
                Log.d(DEBUG_TAG, "onDown: " + event.toString());
                return true;
            }
    
            @Override
            public boolean onFling(MotionEvent event1, MotionEvent event2,
                                   float velocityX, float velocityY) {
                Log.d(DEBUG_TAG, "onFling: " + ((event1 != null) ? event1.toString() : "" + event2.toString()));
                return true;
            }
        }
    }