Question

I am creating an Android app and would like to provide several layouts that can be switched by flinging (like Android home screens). I got the layouts to switch be flinging using this code:

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
            //vFlipper is a ViewFlipper, but it is not important for me to have it. As long as I can display several layouts and switch them by thumb touch, in a way that the layout will follow the thumb, that's fine.
            if (vFlipper.getDisplayedChild() == 0)
                return false;
            vFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
            vFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
            vFlipper.showPrevious();
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
            if (vFlipper.getDisplayedChild() == vFlipper.getChildCount() - 1)
                return false;
            vFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
            vFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
            vFlipper.showNext();
        }
        return true;
    }

but this doesn't work quite like the home screen. On the home screen you can pull the screen to the side and see it moving, not having to complete the fling and switch the screen. The screen moves with your thumb. My code checks for a complete fling gesture and then fires the switching and the animation. Now I realize a full solution will probably drop the GestureDetector and the onFling method and use the onMove event, but I don't know how to implement it and what to put in the onMove method. If you know of a good clear demonstration of this, video or written, a link will be most appreciated. If you don't care writing such a demonstration, that will also be appreciated.

Was it helpful?

Solution

Take a look at the ViewPager class and see if it might meet your needs. It allows you to set up Fragments for each "page" of your content, and swipe to switch between them, with support for the same sort of transition behavior you seem to be describing.

Here's a blog post with more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top