1. 写一个类,实现OnGestureListener, OnTouchListener接口。重写里面的方法,记得把onDown()方法return true;这样才能触发onFling事件。
2. 设置layout的onTouch事件:
LinearLayout layout = (LinearLayout) findViewById(R.id.mylayout); layout.setOnTouchListener(this);
3. 在touch时调用如下:
在onTouch方法中return detector.onTouchEvent(event);
//其中detector是:GestureDetector detector = new GestureDetector(this);
4. 重写onFling()方法:
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX() - e2.getX() > 50 && Math.abs(velocityX)>0){
System.out.println("Left.......");//向左滑动
}else if(e2.getX()-e1.getX()>50 && Math.abs(velocityX)>0){
System.out.println("Right......");//向右滑动
}
return false;
2. 设置layout的onTouch事件:
LinearLayout layout = (LinearLayout) findViewById(R.id.mylayout); layout.setOnTouchListener(this);
3. 在touch时调用如下:
在onTouch方法中return detector.onTouchEvent(event);
//其中detector是:GestureDetector detector = new GestureDetector(this);
4. 重写onFling()方法:
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX() - e2.getX() > 50 && Math.abs(velocityX)>0){
System.out.println("Left.......");//向左滑动
}else if(e2.getX()-e1.getX()>50 && Math.abs(velocityX)>0){
System.out.println("Right......");//向右滑动
}
return false;
}
原文地址:http://net-art.iteye.com/blog/1189285