[英]Android onTouchListener stops receiving events when finger is moved up
I have a custom view with the following OnTouchListener assigned to it in my activity: 我有一个自定义视图,在我的活动中为其分配了以下OnTouchListener:
private OnTouchListener MyOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("onTouch called.");
System.out.println("x" + event.getX() + ", y: " + event.getY());
return true;
}
};
This registers and displays events in the log as expected, until the user moves their finger up or down more than a few pixels. 这会按预期在日志中注册和显示事件,直到用户将手指向上或向下移动超过几个像素。 After this, no touch events are passed to the listener until the user removes and reapplies their finger.
在此之后,在用户移除并重新应用他们的手指之前,不会将任何触摸事件传递给侦听器。 However, using
adb shell getevent
shows that events are still being generated. 但是,使用
adb shell getevent
显示仍在生成事件。 An example of the LogCat output, with annotations, can be found at http://pastebin.com/7EBM2X4V . 可以在http://pastebin.com/7EBM2X4V上找到带注释的LogCat输出示例。
The issue is not that the finger goes outside the bounds of the view. 问题不在于手指超出了视图的范围。
Does anyone know why I have this behaviour? 有谁知道为什么我有这种行为?
Well this might be old, but it still comes up in the search results. 这可能是旧的,但它仍然出现在搜索结果中。 It's possible to prevent the ScrollView from intercepting the touch events by doing this:
通过这样做可以防止ScrollView拦截触摸事件:
private OnTouchListener MyOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
//our touch handling began, request the scrollview not to interact
scrollView.requestDisallowInterceptTouchEvent(true);
System.out.println("Down");
System.out.println("x" + event.getX() + ", y: " + event.getY());
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
System.out.println("Move");
System.out.println("x" + event.getX() + ", y: " + event.getY());
}else if(event.getAction()==MotionEvent.ACTION_UP){
//re-enable it after we're done (finger goes up)
scrollView.requestDisallowInterceptTouchEvent(true);
System.out.println("up");
System.out.println("x" + event.getX() + ", y: " + event.getY());
}
return true;
}
};
It turns out the issue is that the view is contained in a ScrollView. 事实证明,问题是视图包含在ScrollView中。 Removing the ScrollView fixes the problem.
删除ScrollView可以解决问题。
you have to specify the event into the onTouch like down, move or up for that you have to do like this way. 您必须将事件指定为onTouch,例如向下,向上或向上,就像您必须这样做。
private OnTouchListener MyOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
System.out.println("Down");
System.out.println("x" + event.getX() + ", y: " + event.getY());
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
System.out.println("Move");
System.out.println("x" + event.getX() + ", y: " + event.getY());
}else if(event.getAction()==MotionEvent.ACTION_UP){
System.out.println("up");
System.out.println("x" + event.getX() + ", y: " + event.getY());
}
return true;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.