以编程方式模拟平滑的拖动事件 [英] Simulate smooth Drag Event programmatically
问题描述
我使用了自定义 DragLinearLayout 。我使用 addDragView()
添加的所有子级都是可拖动的(用户交互)。
我想为单击的视图模拟拖动事件(顺利移至底部)
I used custom DragLinearLayout. All child I added using addDragView()
are draggable (user interaction).
I want to simulate Drag Event for clicked View (smooth move to the bottom of Layout).
ACTION_DOWN -> ACTION_MOVE -> ACTION_UP
我尝试了此代码,但没有用。
I tried this code, but it didn't work.
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);
推荐答案
我遇到了同样的问题,我不得不模拟
I had the same problem, I had to simulate the listview to overscroll.
经过一些调整后,我成功完成了工作。
After some tweak, I successfully did the work.
这是我所做的: ( overScrollDown()
是我的自定义ListView中的函数)
Here is what I did: (overScrollDown()
is a function in My Custom ListView)
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。使用更多 ACTION_MOVE
事件可以方便地进行向下滚动操作。
Btw. the scroll down operation can be smooth with more ACTION_MOVE
Event..
这篇关于以编程方式模拟平滑的拖动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!