Android                
            タッチイベント
        
        
            
    サーチ…
子ビューと親ビューグループのタッチイベントをどのように変更するか
- ネストされたビューグループの
onTouchEvents()は、booleanonInterceptTouchEventによって管理できます。 
 OnInterceptTouchEventのデフォルト値はfalseです。 
親のonTouchEventは、子の前に受信されます。 OnInterceptTouchEventがfalseを返した場合は、チェーンの下にあるモーションイベントを子のOnTouchEventハンドラに送信します。 trueを返すと、親はtouchイベントを処理します。 
しかし、いくつかの子要素にOnTouchEvent管理させ、あるものを親ビュー(またはおそらく親の親)が管理するようにする場合もあります。 
これは複数の方法で管理することができます。
- 親要素の
OnInterceptTouchEventから子要素を保護する方法の1つは、 requestDisallowInterceptTouchEventを実装することです 。 
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
これにより、要素でイベントハンドラが有効になっている場合、この要素のOnTouchEventを親ビューで管理できなくなります。 
 OnInterceptTouchEventがfalseの場合、子要素のOnTouchEventが評価されます。さまざまなタッチイベントを処理する子要素内にメソッドがある場合、無効になっている関連するイベントハンドラはOnTouchEventを親に返します。 
この答え: 
タッチイベントの伝達がどのように通過するかの視覚化: 
 parent -> child|parent -> child|parent -> child views. 
- 別の方法は、親の
OnInterceptTouchEventからさまざまな値を戻すことです。 
この例では、ViewGroupのタッチイベントの管理からOnTouchEventし、ユーザーがスクロールしているときに子のOnTouchEventをインターセプトする方法を示します。 
4a。
@Override
public boolean onInterceptTouchEvent(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.
     */
    final int 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;
        return false; // Do not intercept touch event, let the child handle it
    }
    switch (action) {
        case MotionEvent.ACTION_MOVE: {
            if (mIsScrolling) {
                // We're currently scrolling, so yes, intercept the 
                // touch event!
                return true;
            }
            // If the user has dragged her finger horizontally more than 
            // the touch slop, start the scroll
            // left as an exercise for the reader
            final int xDiff = calculateDistanceX(ev); 
            // Touch slop should be calculated using ViewConfiguration 
            // constants.
            if (xDiff > mTouchSlop) { 
                // Start scrolling!
                mIsScrolling = true;
                return true;
            }
            break;
        }
        ...
    }
    // In general, we don't want to intercept touch events. They should be 
    // handled by the child view.
    return false;
}
 これは同じリンクのいくつかのコードで、要素の周りの矩形のパラメータを作成する方法を示しています:
4b。
// The hit rectangle for the ImageButton
myButton.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 = new TouchDelegate(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);
}
    
    
    
    
    