简体   繁体   English

当手指向上移动时,Android onTouchListener会停止接收事件

[英]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.

相关问题 手指在按钮视图内时按下按钮,Android OnTouchListener - Press button when finger is inside button view, android OnTouchListener 当父卷轴滚动时,Android View停止接收触摸事件 - Android View stops receiving touch events when parent scrolls Android ImageView和OnTouchListener手指移动 - Android ImageView and OnTouchListener finger move 当手指离开Android中的当前视图时,如何停止接收运动事件的x,y坐标? - How to stop receiving the x, y coordinates of motion events when finger leaves the current view in Android? 使用OnTouchListener,MotionEvent.ACTION_UP时,应用程序意外停止 - App stops unexpectedly when using OnTouchListener, MotionEvent.ACTION_UP On touch Listener在手指移动Android时不会释放按钮 - On Touch Listener does not release button when finger is moved Android 当用户在视图上移动手指时,如何获取在Android的onTouchListener中触摸过的视图的ID? - How can we get the id of the view that was touched in onTouchListener in Android when the user is moving the finger on the views? 如何在Android中使用onTouchListener()与手指一起移动ImageView? - How to move ImageView along with finger using onTouchListener() in android? 使用onTouchListener不能用手指移动视图 - View not moving with finger with onTouchListener Android OnTouchListener一键接收2个事件 - Android OnTouchListener receives 2 events on one click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM