Skip to content

Instantly share code, notes, and snippets.

@CoXier
Created February 21, 2017 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CoXier/066da346bdf19c519457300f7c7988f6 to your computer and use it in GitHub Desktop.
Save CoXier/066da346bdf19c519457300f7c7988f6 to your computer and use it in GitHub Desktop.
Android gesture detector
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
/**
* Created by CoXier on 17-2-21.
*/
public class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final String TAG = "SimpleGestureListener";
private Listener mListener;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i(TAG,e1.toString()+"\n"+e2.toString());
Log.d(TAG,"distanceX = "+distanceX+",distanceY = "+distanceY);
if (mListener == null)
return true;
if (distanceX == 0 && Math.abs(distanceY) > 1){
mListener.onScrollVertical(distanceY);
}
if (distanceY == 0 && Math.abs(distanceX) > 1){
mListener.onScrollHorizontal(distanceX);
}
return true;
}
public void setListener(Listener mListener) {
this.mListener = mListener;
}
interface Listener{
/**
* left scroll dx >0
* right scroll dx <0
* @param dx
*/
void onScrollHorizontal(float dx);
/**
* upward scroll dy > 0
* downward scroll dy < 0
* @param dy
*/
void onScrollVertical(float dy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment