• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with ViewFlipper and TouchEvent

eafkuor

Lurker
Mar 19, 2010
7
0
Hi everyone, in my app there's a ViewFlipper that shows 3 different layouts, and the user can switch between them (the new one comes in from the left, the old one goes out to the right, and vice versa) by using two Buttons.
I wanted to give the user a chance to simply press the screen, move the finger, and release it, to switch between the same layouts.
So I attached the ViewFlipper to this onTouchListener:

Code:
public boolean onTouch(View v, MotionEvent event) {
        
        float startx=0, endx=0;
        ViewFlipper flipper=(ViewFlipper)v;

        
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startx=event.getRawX();
                break;
                
            case MotionEvent.ACTION_MOVE:
                break;
                
            case MotionEvent.ACTION_UP:
                endx=event.getRawX();
                if(endx-startx > 50){
                    //move to the left
                    flipper.setInAnimation(Animations.inFromLeftAnimation());
                    flipper.setOutAnimation(Animations.outToRightAnimation());
                    flipper.showPrevious(); 
                }
                else if(startx-endx > 50){
                    //move to the right
                    flipper.setInAnimation(Animations.inFromRightAnimation());
                    flipper.setOutAnimation(Animations.outToLeftAnimation());
                    flipper.showNext();
                }
                break;
        }

If I call the same methods on flipper in the Buttons' onClick() callback, the animation works fine.
But in this, it doesn't work right, it always moves to the left. And it also moves if you just click on the screen without moving the finger (it should move only if you moved the finger more than 50 pixels).
Do you have an idea why?
Thanks!
Simone
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones