Skip to content

Instantly share code, notes, and snippets.

@jkreiser
Created April 1, 2014 15:25
Show Gist options
  • Save jkreiser/9916464 to your computer and use it in GitHub Desktop.
Save jkreiser/9916464 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FF0000"
android:id="@+id/myView"/>
</RelativeLayout>
package com.example.SlidingGestureExample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
View myView = (View) findViewById(R.id.myView);
myView.setOnTouchListener(new OnSlidingTouchListener(this) {
@Override
public boolean onSlideLeft() {
// do something
return true;
}
@Override
public boolean onSlideRight() {
// do something
return true;
}
@Override
public boolean onSlideUp() {
// do something
return true;
}
@Override
public boolean onSlideDown() {
// do something
return true;
}
});
}
}
package com.example.SlidingGestureExample;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSlidingTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSlidingTouchListener(Context context){
gestureDetector = new GestureDetector(context, new GestureListener());
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private final String TAG = GestureListener.class.getSimpleName();
private static final int SLIDE_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return onClick();
}
@Override
public void onLongPress(MotionEvent e) {
onLongClick();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
try {
float deltaY = e2.getY() - e1.getY();
float deltaX = e2.getX() - e1.getX();
if (Math.abs(deltaX) > Math.abs(deltaY)) {
if (Math.abs(deltaX) > SLIDE_THRESHOLD) {
if (deltaX > 0) {
// the user made a sliding right gesture
return onSlideRight();
} else {
// the user made a sliding left gesture
return onSlideLeft();
}
}
} else {
if (Math.abs(deltaY) > SLIDE_THRESHOLD) {
if (deltaY > 0) {
// the user made a sliding down gesture
return onSlideDown();
} else {
// the user made a sliding up gesture
return onSlideUp();
}
}
}
} catch (Exception exception) {
Log.e(TAG, exception.getMessage());
}
return false;
}
}
public boolean onClick() {
return false;
}
public boolean onLongClick() {
return false;
}
public boolean onSlideRight() {
return false;
}
public boolean onSlideLeft() {
return false;
}
public boolean onSlideUp() {
return false;
}
public boolean onSlideDown() {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment