开发者

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:

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 genera开发者_运维问答ted. 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?


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜