我有一个有2页的ViewPager。其中一个是自定义视图以及其他一些小部件。我想要的是如果该自定义视图使用了触摸事件,则禁用分页。
以下是我的自定义视图中的相关代码:
class OscilloscopeView extends View implements GestureDetector.OnGestureListener {
OscilloscopeView(Context context){
mGestureDetector = new GestureDetectorCompat(context, this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event) || mGestureDetector.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event) || mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent motionEvent) {
Log.d("MDETECTOR", "onDown: " + motionEvent.toString());
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
int dHPos = (int) (distanceX / mWidth * points.length / 2);
if (Utilities.around(hPosition - dHPos, 0, points.length / 4))
hPosition -= dHPos;
triggerLevel += (2 * distanceY / mHeight * max);
invalidate();
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
hPosition = 0;
triggerLevel = 0;
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
Log.d("MDETECTOR", "onFling: " + motionEvent.toString());
return true;
}
}
ViewPager是正常的,没有覆盖任何方法。因此,我必须覆盖哪些方法以及如何滚动事件位于视图区域内,该事件不再传递给ViewPager,而如果在视图外滑动,则ViewPager正常工作。我猜我不应该在某些方法上打电话超级,但我不确定。
答案 0 :(得分:1)
@Override
public boolean onTouchEvent(MotionEvent event) {
ViewParent parent = getParent();
parent.requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event) || mGestureDetector.onTouchEvent(event);
}