Android onTouchListener stops receiving events when finger is moved up

随声附和 提交于 2020-01-06 08:48:34

问题


I have a custom view with the following OnTouchListener assigned to it in my activity:

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. An example of the LogCat output, with annotations, can be found at http://pastebin.com/7EBM2X4V.

The issue is not that the finger goes outside the bounds of the view.

Does anyone know why I have this behaviour?


回答1:


It turns out the issue is that the view is contained in a ScrollView. Removing the ScrollView fixes the problem.




回答2:


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:

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;
    }
};



回答3:


you have to specify the event into the onTouch like down, move or up for that you have to do like this way.

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;
    }
};


来源:https://stackoverflow.com/questions/7306092/android-ontouchlistener-stops-receiving-events-when-finger-is-moved-up

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