10

Is there something like onLeftSwipeListener and onRightSwipeListener in Android? I want to switch views swiping finger back and forward. I use a FrameLayout and an ImageView inside inside of it.

lomza
  • 9,412
  • 15
  • 70
  • 85

4 Answers4

14

Its called GestureDetector and SimpleOnGestureListener which has onFling(). Its called fling and not swipe in this context :)

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • 2
    Oh, I see. But can it distinguish between flinging to left and flinging to right? – lomza May 12 '11 at 09:33
  • 1
    You can find details here: http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float) – dst May 12 '11 at 09:37
  • and one more question. whould it be enough to use intents in order to switch views or it is a dumb way? – lomza May 12 '11 at 10:07
7
yourView.setOnTouchListner(onThumbTouch );

    OnTouchListener onThumbTouch = new OnTouchListener() {
            float previouspoint = 0 ;
            float startPoint=0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {   
                switch(v.getId()) {
                    case R.id.tvDetailsalaujairiyat: // Give your R.id.sample ...
                        switch(event.getAction()){
                            case MotionEvent.ACTION_DOWN:
                                startPoint=event.getX();
                                System.out.println("Action down,..."+event.getX());
                            break;
                            case MotionEvent.ACTION_MOVE:

                            break;
                            case MotionEvent.ACTION_CANCEL:
                                previouspoint=event.getX();
                                if(previouspoint > startPoint){
                                   //Right side swipe        
                                }else{
                                   // Left side swipe
                                }        
                            break;
                       }
                    break;
                }
                return true;
            }
        };
Patrick Aleman
  • 612
  • 6
  • 19
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
6

i have made a small example of swiper in android i would like share the code with you.

Chek this.

//LAYOUT///

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Soy A"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Soy B"
            android:textColor="#BBBFFF" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Soy C"
            android:textColor="#FFBBFF" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Soy D"
            android:textColor="#FFFFAF" />
    </ViewFlipper>

</LinearLayout>

///ACTIVITY///

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class SwipeActivity extends Activity {

    private Animation mInFromRight;
    private Animation mOutToLeft;
    private Animation mInFromLeft;
    private Animation mOutToRight;
    private ViewFlipper mViewFlipper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
        mViewFlipper.setDisplayedChild(0);
        initAnimations();
    }

    private void initAnimations() {
        mInFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        mInFromRight.setDuration(500);
        AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
        mInFromRight.setInterpolator(accelerateInterpolator);

        mInFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        mInFromLeft.setDuration(500);
        mInFromLeft.setInterpolator(accelerateInterpolator);

        mOutToRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                0.0f, Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        mOutToRight.setDuration(500);
        mOutToRight.setInterpolator(accelerateInterpolator);

        mOutToLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        mOutToLeft.setDuration(500);
        mOutToLeft.setInterpolator(accelerateInterpolator);

        final GestureDetector gestureDetector;
        gestureDetector = new GestureDetector(new MyGestureDetector());

        mViewFlipper.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return false;
                } else {
                    return true;
                }
            }
        });
    }

    private class MyGestureDetector extends SimpleOnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            System.out.println(" in onFling() :: ");
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                mViewFlipper.setInAnimation(mInFromRight);
                mViewFlipper.setOutAnimation(mOutToLeft);
                mViewFlipper.showNext();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                mViewFlipper.setInAnimation(mInFromLeft);
                mViewFlipper.setOutAnimation(mOutToRight);
                mViewFlipper.showPrevious();
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }
}
shontauro
  • 1,812
  • 1
  • 25
  • 26
0

Here's a related quetion about implementing gestures detection:

Fling gesture detection on grid layout

You could also use a ViewFlipper with some animation to switch between the views, it looks good, here's an example implementation for enabling left/right swipe on a viewflipper:

http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html

Community
  • 1
  • 1
BFil
  • 12,966
  • 3
  • 44
  • 48
  • Can I do fling (swipe) on my emulator? – lomza May 12 '11 at 13:27
  • Yes, there shouldn't be problems – BFil May 12 '11 at 13:33
  • I used Gav's code snippet, but doesn't matter if I flip or just click, I always get a click. How can I have both of them? How can I distiguish between them? – lomza May 12 '11 at 13:55
  • I tried to fling on the emulator, just to be sure it worked on it, and it works well, maybe you're missing something, check your code aginst the one from gav, it works for me. There are also some issues when you need to detect the fling inside a scrollview, is it your case? – BFil May 12 '11 at 14:12
  • Thanks, now it works. I have a pic. It consumes all my screen.When I swip from right to left I have to change a pic. So I call an intent. But it does not work. Here is the code: `public SwipeDetector(Activity activity, Class called_class){ this.activity = activity; this.called_class = called_class; }` Then I have `public void onRightToLeftSwipe(){ Intent i = new Intent(activity, called_class); startActivity(i); }` and in my activity: `SwipeDetector swipeDetector = new SwipeDetector(this, SecondScreen.class); myView.setOnTouchListener(activitySwipeDetector);` – lomza May 12 '11 at 14:47