Simulate smooth Drag Event programmatically

Deadly 提交于 2019-12-04 11:51:09

I had the same problem, I had to simulate the listview to overscroll.

After some tweak, I successfully did the work.

Here is what I did: (overScrollDown() is a function in My Custom ListView)

You can see detail here in this gist. Also the 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);
}

Btw. the scroll down operation can be smooth with more ACTION_MOVE Event..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!