将OnTouchListener用于拖放操作时设置延迟

时间:2013-02-23 17:18:57

标签: android drag-and-drop onclicklistener ontouchlistener tablerow

我有一个包含表行的项目,可以通过拖放操作将其移动到其他表。我希望这些行也是可突出的,我将focusable和focusableontouchmode设置为true,将list_selector_background设置为背景,并使行可单击。没有OnTouchListener选择工作正常,但如果我将onTouchListener设置为行,它会立即启动拖放过程,当我点击它。有没有办法设置onTouchListener的延迟,以便它可以区分单击和拖放进程的开始? (这样你就必须按住行开始拖动才能开始拖动)

OnTouchListener:

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }

    }
}

The Row:

teacherRow.setId(tid);
teacherRow.setBackgroundResource(drawable.list_selector_background);
teacherRow.setFocusable(true);
teacherRow.setFocusableInTouchMode(true);
teacherRow.setClickable(true);
teacherRow.setOnTouchListener(new MyTouchListener());

我也尝试使用行上的OnClickListener来解决它,但它不起作用

1 个答案:

答案 0 :(得分:0)

  

有没有办法设置onTouchListener的延迟以便它   可以区分单击和拖放的开始   过程

我不确定我完全理解你想做什么,但看看下面的代码:

        public boolean onTouch(final View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        if (view.isPressed()) {
                            ClipData data = ClipData.newPlainText("", "");
                            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                                    view);
                            view.startDrag(data, shadowBuilder, view, 0);
                            v.setVisibility(View.INVISIBLE);
                        }
                    }

                }, (long) (1.5 * ViewConfiguration.getLongPressTimeout()));
            }
            return false;
        }

mHandler是一个简单的Handler实例。