2013-07-12 36 views
1

我有兩個視圖:一個子視圖和一個父視圖(子視圖是父視圖的一部分);每個視圖都有自己的onTouchListener。我希望每個視圖都能處理它自己的onInterceptTouchEvent。馬上;但是,子視圖的onTouchListener從不接收任何事件。他們都被髮送到父視圖的偵聽器。我怎樣才能確保子視圖收到自己的事件?多次處理運動事件

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    //Log.d(TAG, "onInterceptTouchEvent " + ev); 
    // handle child events 
    // note: if you horizontally fling over button its onClick() is not performed 


     MotionEvent ss = MotionEvent.obtain(ev); 
     globalVariabels.currentGallery.onTouchEvent(ss); 
     int bounds[] = {0,0}; 
     globalVariabels.currentGallery.getLocationOnScreen(bounds); 
     int height = globalVariabels.currentGallery.getHeight() + bounds[0]; 
     int width = globalVariabels.currentGallery.getWidth() + bounds[1]; 
     TouchDelegate delegate = new TouchDelegate(new Rect(0, 0, 2000, 2000), globalVariabels.currentGallery); 
     setTouchDelegate(delegate); 
     switch (ev.getAction()) { 
      case MotionEvent.ACTION_DOWN: { 
       mIgnore = false; 
       Log.e("ooo", "oo"); 
       Log.e("X:","" + ev.getX()); 
       Log.e("Y:","" + ev.getY()); 
       mNeedToRebase = true; 
       mInitialX = ev.getX(); 
       mInitialY = ev.getY(); 
       return false ; 
      } 

      case MotionEvent.ACTION_MOVE: { 
       if (!mIgnore) { 
        Log.e("22", "22"); 
        float deltaX = Math.abs(ev.getX() - mInitialX); 
        float deltaY = Math.abs(ev.getY() - mInitialY); 
        mIgnore = deltaX < deltaY; 
        super.onInterceptTouchEvent(ev); 
        return !mIgnore; 
       } 
       return false; 
      } 
      default: { 
       return super.onInterceptTouchEvent(ev); 
      } 
     } 
    } 
+0

和你的問題是......? – pskink

+0

當我嘗試滑動孩子時,父母處理該事件。 – user2357536

+0

看到我的答案在這裏:http://stackoverflow.com/questions/16979285/how-to-dispatch-touch-events-to-children-after-consuming – pskink

回答

0

考慮使用dispatchTouchEvent而不是onInterceptTouchEvent。我曾經遇到過一些問題,因爲在某些設備上(更準確地說三星電子),它可能不像預期的那樣工作。調度真的很簡單。

的想法是,你重寫dispatchTouchEvent,超級調用和處理您的移動事件。這裏是一個例子:

@Override 
protected boolean dispatchTouchEvent(MotionEvent ev) { 
    //perform your on-touch behaviour 

    return super.dispatchTouchEvent(ev); //child's onTouchListener will be triggered 
}