以编程方式模拟平滑拖动事件

时间:2016-11-22 16:43:14

标签: java android drag-and-drop touch motionevent

我使用了自定义DragLinearLayout。我使用addDragView()添加的所有孩子都是可拖动的(用户互动) 我想为点击的视图模拟拖动事件(平滑移动到布局的底部)。

ACTION_DOWN -> ACTION_MOVE -> ACTION_UP

我尝试了这段代码,但它没有用。

long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
float x = view.getLeft();
float y = view.getTop();
int metaState = 0;
MotionEvent downEvent = MotionEvent.obtain(
                            downTime,
                            eventTime + 1000,
                            MotionEvent.ACTION_DOWN,
                            x,
                            y,
                            metaState
                    );
view.dispatchTouchEvent(downEvent);

MotionEvent moveEvent = MotionEvent.obtain(
                            downTime,
                            eventTime + 1000,
                            MotionEvent.ACTION_MOVE,
                            x,
                            y + 300,
                            metaState
                    );
view.dispatchTouchEvent(moveEvent);

MotionEvent upEvent = MotionEvent.obtain(
                            downTime,
                            eventTime + 1001,
                            MotionEvent.ACTION_UP,
                            x,
                            y + 300,
                            metaState
                    );
view.dispatchTouchEvent(upEvent);

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我不得不模拟listview过度滚动。

经过一些调整后,我成功完成了工作。

以下是我所做的:( overScrollDown()是My Custom ListView中的一个函数)

您可以在此gist中查看详细信息。还有screencast

public void overScrollDown() {
    post(new Runnable() {
        @Override
        public void run() {
            final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, getWidth() / 2, getHeight() / 2, 0);
            dispatchTouchEvent(event);
            event.recycle();
        }
    });

    postDelayed(new Runnable() {
        @Override
        public void run() {
            final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, getWidth() / 2, getHeight() / 2, 0);
            dispatchTouchEvent(event);
            event.recycle();
        }
    }, 50);

    postDelayed(new Runnable() {
        @Override
        public void run() {
            final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, getWidth() / 2, getHeight() / 2 + 400, 0);
            dispatchTouchEvent(event);
            event.recycle();
        }
    }, 100);

    postDelayed(new Runnable() {
        @Override
        public void run() {
            final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, getWidth() / 2, getHeight() / 2 + 400, 0);
            dispatchTouchEvent(event);
            event.recycle();
        }
    }, 3000);
}

顺便说一下。向下滚动操作可以更顺畅,更多ACTION_MOVE事件..