2014-01-22 45 views
1

登錄從未登錄ACTION_UPACTION_MOVE(這是我從代碼示例中刪除縮短)onInterceptTouchEvent的ACTION_UP和ACTION_MOVE不會被調用

這裏是我的代碼的縮短版:

public class ProfileBadgeView extends LinearLayout { 

    Activity act; 

    public ProfileBadgeView(Context context) { 
     super(context); 
    } 

    public ProfileBadgeView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public ProfileBadgeView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void initView(Activity act) { 
     //..init 
    } 


    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 

     if (ev.getAction() == MotionEvent.ACTION_DOWN) { 
      logIntercept("ACTION DOWN"); 
     } else if (ev.getAction() == MotionEvent.ACTION_UP) { 
      logIntercept("ACTION_UP"); 
     } 
     return false; 
    } 

    @Override 
     public boolean onTouchEvent(MotionEvent ev) { 
     return true; 
} 


    private void logIntercept(Object obj) { 
     Log.i(this.getClass().getSimpleName() + " INTERCEPT :", obj.toString()); 
    } 



} 
+0

將敬酒的條件之外,並檢查是否顯示敬酒 –

回答

8

您的onInterceptTouchEvent方法不會在ACTION_DOWN事件後調用,因爲您在onTouchEvent方法中返回true。因此,所有的其他事件中onTouchEvent發送,而不是在任何onInterceptTouchEvent更多:

使用此功能,則需要小心一些,因爲它與View.onTouchEvent(MotionEvent)一個相當複雜的 互動,並使用它需要 以正確的方式執行該方法以及此方法。 活動將按以下順序收到:

您將在此處收到停機事件。 down事件將由該視圖組的子項處理 ,或者發送給您自己的 onTouchEvent()方法來處理;這意味着您應該實現 onTouchEvent()以返回true,因此您將繼續看到 其餘的手勢(而不是查找父視圖來處理它)。此外, 通過的onTouchEvent返回true(),您將不會收到onInterceptTouchEvent像正常任何 下列事件()和所有觸摸處理 必須的onTouchEvent發生()。

http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)