设置触摸事件可以在主activity中设置一个接口如下
然后再在fragment中实例化一个mainactivity.myOntouchlistener
private ArrayList<MyOnTouchListener> onTouchListeners = new ArrayList<MyOnTouchListener>(
10);
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
for (MyOnTouchListener listener : onTouchListeners) {
listener.onTouch(ev);
}
return super.dispatchTouchEvent(ev);
}
public void registerMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
onTouchListeners.add(myOnTouchListener);
}
public void unregisterMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
onTouchListeners.remove(myOnTouchListener) ;
}
public interface MyOnTouchListener {
public boolean onTouch(MotionEvent ev);
}
然后再在fragment中实例化一个mainactivity.myOntouchlistener
myOnTouchListener=new MainActivity.MyOnTouchListener() {
@Override
public boolean onTouch(MotionEvent ev) {
boolean result = mGestureDetector.onTouchEvent(ev);
return result;
}
};
((MainActivity)getActivity()).registerMyOnTouchListener(myOnTouchListener);