publicclassMyViewGroupextendsViewGroup {privateint mTouchSlop;...ViewConfiguration vc =ViewConfiguration.get(view.getContext()); mTouchSlop =vc.getScaledTouchSlop();... @OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {/* * This method JUST determines whether we want to intercept the motion. * If we return true, onTouchEvent will be called and we do the actual * scrolling there. */finalint action =MotionEventCompat.getActionMasked(ev);// Always handle the case of the touch gesture being complete.if (action ==MotionEvent.ACTION_CANCEL|| action ==MotionEvent.ACTION_UP) {// Release the scroll. mIsScrolling =false;returnfalse; // Do not intercept touch event, let the child handle it }switch (action) {caseMotionEvent.ACTION_MOVE: {if (mIsScrolling) {// We're currently scrolling, so yes, intercept the// touch event!returntrue; }// If the user has dragged her finger horizontally more than// the touch slop, start the scroll// left as an exercise for the readerfinalint xDiff =calculateDistanceX(ev);// Touch slop should be calculated using ViewConfiguration// constants.if (xDiff > mTouchSlop) {// Start scrolling! mIsScrolling =true;returntrue; }break; }... }// In general, we don't want to intercept touch events. They should be// handled by the child view.returnfalse; } @OverridepublicbooleanonTouchEvent(MotionEvent ev) {// Here we actually handle the touch event (e.g. if the action is ACTION_MOVE,// scroll this container).// This method will only be called if the touch event was intercepted in// onInterceptTouchEvent... }}
publicclassMainActivityextendsActivity { @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Get the parent viewView parentView =findViewById(R.id.parent_layout);parentView.post(newRunnable() {// Post in the parent's message queue to make sure the parent// lays out its children before you call getHitRect() @Overridepublicvoidrun() {// The bounds for the delegate view (an ImageButton// in this example)Rect delegateArea =newRect();ImageButton myButton = (ImageButton) findViewById(R.id.button);myButton.setEnabled(true);myButton.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View view) {Toast.makeText(MainActivity.this,"Touch occurred within ImageButton touch region.",Toast.LENGTH_SHORT).show(); } });// The hit rectangle for the ImageButtonmyButton.getHitRect(delegateArea);// Extend the touch area of the ImageButton beyond its bounds// on the right and bottom.delegateArea.right+=100;delegateArea.bottom+=100;// Instantiate a TouchDelegate.// "delegateArea" is the bounds in local coordinates of// the containing view to be mapped to the delegate view.// "myButton" is the child view that should receive motion// events.TouchDelegate touchDelegate =newTouchDelegate(delegateArea, myButton);// Sets the TouchDelegate on the parent view, such that touches// within the touch delegate bounds are routed to the child.if (View.class.isInstance(myButton.getParent())) { ((View) myButton.getParent()).setTouchDelegate(touchDelegate); } } }); }}