Android,DragListener结合Single Tap或OnClick / OnTouch

时间:2015-12-24 19:57:52

标签: android drag

我正在努力将DragListener与SingeTap动作结合在一个imageView中。他们分开工作但这两者应该如何组合?

imageView.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch(event.getAction()) {
                        case MotionEvent.ACTION_UP:
                            Log.e("SINGLE TAP","?");
                            break;
                        case MotionEvent.ACTION_MOVE:
                            ClipData data = ClipData.newPlainText("TRYING", "");
                            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                            v.startDrag(data, shadowBuilder, v, 0);
                            break;
                        default: break;
                    }
                    return false;
                }
            });

似乎仍然无法运作。如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

在拖动事件结束后以及点击事件结束后,将调用

ACTION_UP。因此,您应该有一个标志,用于检测是否发生拖动事件。这是一个例子

imageView.setOnTouchListener(new OnTouchListener() {
            private boolean isDrag = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (!isDrag) {
                            // action was a single tap
                        }
                        isDrag = false; // reset the flag
                        break;
                    case MotionEvent.ACTION_MOVE:
                        isDrag = true; // set the flag
                        ClipData data = ClipData.newPlainText("TRYING", "");
                        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                        v.startDrag(data, shadowBuilder, v, 0);
                        break;
                    default: break;
                }
                return false;
            }
        });