Dray
Dray

Reputation: 1954

swipe animation in layouts: android

I know the animation effects and how to apply them to views(layouts) for onclicklistener, ontouchlistener etc. but which event occurs when i slide the layout to left or right.

previousButton.setOnClickListener(new OnClickListener()  {
      @Override 
        publicvoid onClick(View v) {  
              viewFlipper.setInAnimation(MainActivity.this,R.anim.view_transition_in_right);
               viewFlipper.setOutAnimation(MainActivity.this,R.anim.view_transition_out_right);
               viewFlipper.showPrevious();  
      }  
    });  
}

i dont want onclicklistener. ie)if i touch and slide the current layout to left, it should go out and next layout should be in current view.how to do this?

Upvotes: 0

Views: 2063

Answers (3)

Deva
Deva

Reputation: 3949

Make your activity implement the onGestureListener.

then in you activity add

private GestureDetector gestureScanner;
gestureScanner = new GestureDetector(this);
private static final int SWIPE_MIN_DISTANCE = 100;
    private static final int SWIPE_THRESHOLD_VELOCITY = 100;

Then overrid the onTouchEvent(MotionEvent event) like below:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureScanner.onTouchEvent(event);
    }

Also Finally you need to overide onFling method something like:

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        /* on scroll to the next page */
        if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
                ) {
            //Ur code goes here
        }
        /* on scroll to the previous page  */
        else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
                ) {
            //ur code goes here.
        }
        return false;
    }

Hope this helps.

Upvotes: 1

Dean Wild
Dean Wild

Reputation: 5954

You need the View pager -

ViewPager

Very easy to implement too, you just provide an adapter in a similar way that you would with a ListView.

Upvotes: 0

colegu
colegu

Reputation: 370

You should use Fragments for sliding between views. Use FragmentActivity and Fragment(for each view).android developers example

Or you can just use this opensource library

Upvotes: 0

Related Questions