onTouchListener是用来实现View对触摸事件的接口,实现这个接口需要定义函数public boolean onTouch(View v, MotionEvent event),touchevent 是一个笼统的称呼,其实包含有多种动作的组合,例如上下滚动,左右滑动,轻点以及长按等等。那如何去根据不同的动作来进行响应的处理,通常在onTouch函数里返回一个GestureDetector实例的onTouchEvent方法。可以认为是由GestureDetector这个类来处理不同的触摸动作,GestureDetector的构造函数有
public GestureDetector (Context context, GestureDetector.OnGestureListener listener)
GestureDetector.OnGestureListener 是一个接口,需要实现接口里的方法,这些方法包括
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
public boolean onSingleTapUp(MotionEvent ev)
public void onShowPress(MotionEvent ev) {
Log.d("DEBUG","onShowPress");
}
@Override
public void onLongPress(MotionEvent ev) {
Log.d("DEBUG","onLongPress");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.d("DEBUG","onScroll");
return true;
}
@Override
public boolean onDown(MotionEvent ev) {
Log.d("DEBUG","onDownd");
return true;
}
可以很明显的知道这些个函数就是来对不同触摸动作的处理