在开发过程当中可能会遇到诸如此类问题:android
一、在上下滑动的ScrollView中嵌套一个横滑列表,拖动横滑列表时可能引发ScrollView的上下滑动致使体验极差this
二、在ViewPager中嵌套了一个横滑列表,在拖动横滑列表时一样可能致使ViewPager的tab切换。事件
requestDisallowInterceptTouchEvent 是ViewGroup类中的一个公用方法,参数是一个boolean值,官方介绍以下开发
when a child does not want this parent and its ancestors to intercept touch events with ViewGroup.onInterceptTouchEvent(MotionEvent).get
This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.it
android系统中,一次点击事件是从父view传递到子view中,每一层的view能够决定是否拦截并处理点击事件或者传递到下一层,若是子view不处理点击事件,则该事件会传递会父view,由父view去决定是否处理该点击事件。在子view能够经过设置此方法去告诉父view不要拦截并处理点击事件,父view应该接受这个请求直到这次点击事件结束。io
实际的应用中,能够在子view的ontouch事件中注入父ViewGroup的实例,并调用requestDisallowInterceptTouchEvent去阻止父view拦截点击事件event
public boolean onTouch(View v, MotionEvent event) {
ViewGroup viewGroup = (ViewGroup) v.getParent();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
viewGroup.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
viewGroup .requestDisallowInterceptTouchEvent(false);
break;
}
}
request