0

My intention is to implement swipe in a layout. On swiping from left or right in the layout, one view will be visible and hidden.

Am using Android Studio. Is there any library available which can be used directly?

halfer
  • 19,824
  • 17
  • 99
  • 186
dreamdeveloper
  • 1,266
  • 1
  • 15
  • 20
  • 1
    You could have a look at the ViewPager. See for example [Google's documentation](https://developer.android.com/training/animation/screen-slide.html). – Markus Kauppinen Oct 05 '15 at 12:10
  • I just need swipe inside a small layout in a page. just like u have two images in box, on swipe from left to right one will be displayed and other will be partial and vice versa. I am not able to upload an screenshot here. – dreamdeveloper Oct 05 '15 at 12:32
  • i think you are trying to implement a layout like tinder.right? – Vipul Asri Oct 05 '15 at 14:12
  • yes..i am not able to upload a screenshot over here.. – dreamdeveloper Oct 05 '15 at 14:17
  • i am basically looking for a simple implementation for the question specified in the below link http://stackoverflow.com/questions/30908068/android-listview-swipe-right-and-left-to-accept-and-reject – dreamdeveloper Oct 06 '15 at 14:01

1 Answers1

1

You can use onfling gesture listener. Sample code for you given below

public class MyActivity extends Activity {
private void onCreate() {
 // Set your layout
 final ImageView imageView = (ImageView) findViewById(R.id.image_view);
 imageView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(final View view, final MotionEvent event) {
       return gdt.onTouchEvent(event);
    }
 });
  }

  private final GestureDetector gdt = new GestureDetector(new     GestureListener());

  private class GestureListener extends SimpleOnGestureListener {

 private final int SWIPE_MIN_DISTANCE = 120;
 private final int SWIPE_THRESHOLD_VELOCITY = 200;

 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) >     SWIPE_THRESHOLD_VELOCITY) {
       // Right to left, your code here
       return true;
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE &&     Math.abs(velocityX) >  SWIPE_THRESHOLD_VELOCITY) {
       // Left to right, your code here
       return true;
    }
    if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) >     SWIPE_THRESHOLD_VELOCITY) {
       // Bottom to top, your code here
       return true;
    } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE &&    Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
       // Top to bottom, your code here
       return true;
    }
    return false;
   }
  }
  }
Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32