简体   繁体   中英

action_up doesn't call after action_move

I have an animation that makes a button press down on touch. I want it to be able to stay pressed down, while the user scrolls the screen. To do this, I made an animation that basically just keeps the image in the same state to call in the ACTION_MOVE. My problem is that when I release the button, the ACTION_UP isn't being called which is what triggers the animation to bring the button back to it's full size and no longer looking pressed. I have the entire relative layout inside a ScrollView. I still don't understand fully what's happening, but it appears that the ScrollView may be "taking over" the onTouch events. Here is my code for the onTouchListener.

s104.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                clearAnim();
                s104.setAnimation(animPressed);
                s104.startAnimation(animPressed);
                return true;
            case MotionEvent.ACTION_MOVE:
                s104.setAnimation(animDrag);
                s104.startAnimation(animDrag);
                return true;
            case MotionEvent.ACTION_UP:

                s104.setAnimation(animReleased);
                s104.startAnimation(animReleased);

                return true;
            }
            return false;
        }
    });

I can't figure out why the ACTION_UP isn't being called. Any help would be greatly appreciated.

I still don't understand fully what's happening, but it appears that the ScrollView may be "taking over" the onTouch events.

That is correct. What you are experiencing is the ScrollView "taking over" the MotionEvent . You can capture this event in your OnTouchListener - add a case for MotionEvent.ACTION_CANCEL . In there you can determine the outcome if the user has pressed the View (ACTION_DOWN) and subsequently scrolled down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM