Android 自定义视图容器控件拦截ontouch事件传递给其子控件

时间:2022-12-01 22:25:19

自定义View 控件:

/*
 * 
 * 一个视图容器控件
 * 阻止 拦截 ontouch事件传递给其子控件
 * */
public class InterceptScrollContainerView extends LinearLayout {

	public InterceptScrollContainerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public InterceptScrollContainerView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
//	
//	@Override
//	public boolean dispatchTouchEvent(MotionEvent ev) {
//		// TODO Auto-generated method stub
//		//return super.dispatchTouchEvent(ev);
//		Log.i("pdwy","ScrollContainer dispatchTouchEvent");
//		return true;
//	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		//return super.onInterceptTouchEvent(ev);
		Log.i("pdwy","ScrollContainer onInterceptTouchEvent");
		return true;
		
		//return super.onInterceptTouchEvent(ev);
	}
	
//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		// TODO Auto-generated method stub
//		Log.i("pdwy","ScrollContainer onTouchEvent");
//		return true;
//	}
}

实现只需用其包裹子控件就OK!