1、点击事件:
touch的动作:
ACTION_DOWN //按下
ACTION_MOVE //移动
ACTION_UP //离开屏幕
ACTION_CANCEL //取消,不会由用户产生,而是由程序产生的
ViewGroup类会处理点击事件的主要有3个方法分别是:
public boolean dispatchTouchEvent(MotionEvent ev) //用于事件的分发(一般不复写,复写之后返回true则按着屏幕会一直执行,返回false按着屏幕只执行一次。但返回true和false都不会有事件传递给onInterceptTouchEvent。)
public boolean onInterceptTouchEvent(MotionEvent ev) // 用于事件的拦截(负责事件向下传递的方向,返回false则传递给下一层的dispatch方法。返回true则动作交给自己的onTouchEvent处理,后续事件不再经过,直接由dispatch传递给onTouchEvent。)
public boolean onTouchEvent(MotionEvent ev) //处理事件(负责事件向上的传递)
view类处理点击事件的方法只有两个:
public boolean dispatchTouchEvent(MotionEvent ev) //用于事件的分发
public boolean onTouchEvent(MotionEvent ev) //处理事件
2、监听
比较有用的监听事件有:GestureDetector能比较优雅处理点击事件
public class LoopRelativeLayout extends RelativeLayout {
private GestureDetector mGesture;
public void setOnSingleTouchListener(OnSingleTouchListener mSingleTouch) {
this.mSingleTouch = mSingleTouch;
}
public LoopRelativeLayout(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mGesture = new GestureDetector(context, listener);
}
public LoopRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mGesture = new GestureDetector(context, listener);
}
public LoopRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mGesture = new GestureDetector(context, listener);
}
public LoopRelativeLayout(Context context) {
super(context);
mGesture = new GestureDetector(context, listener);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
mGesture.onTouchEvent(event);
return true;
}
private GestureDetector.OnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
Common.showsyso("onDown:手指点击屏幕就执行");
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Common.showsyso("onSingleTapUp:单击 onDown之后执行");
return false;
};
@Override
public void onShowPress(MotionEvent e) {
Common.showsyso("onShowPress:点击不抬起手指 onDown之后执行");
};
@Override
public void onLongPress(MotionEvent e) {
Common.showsyso("onLongPress:点击不抬起手指 onShowPress之后执行");
};
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Common.showsyso("onScroll:接触屏幕之后之后手指滑动 onDown之后执行");
return false;
};
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Common.showsyso("onFling:onScroll之后执行");
return false;
};
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Common.showsyso("单击 ");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Common.showsyso("双击 中的其中一下");
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Common.showsyso("双击 ");
return false;
}
};
}
还有一个RotateGestureDetector监听:是用来处理手势的,缩放啥的。时间有限以后再详细的研究。