ViewGroup में टच इवेंट मैनेज करना

टच इवेंट को ViewGroup का खास ख्याल है क्योंकि ViewGroup में बच्चे आम तौर पर अलग-अलग तरह के बच्चों के टारगेट होते हैं ViewGroup के अलावा अन्य सभी तरह के टच इवेंट. यह पक्का करने के लिए कि हर व्यू को सही तरीके से इसके लिए लक्षित टच इवेंट को ओवरराइड करता है, तो onInterceptTouchEvent() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है तरीका.

किसी ViewGroup में इंटरसेप्ट टच इवेंट

किसी टच इवेंट का पता चलने पर, onInterceptTouchEvent() तरीका कॉल किया जाता है ViewGroup की सतह. इसमें बच्चों की सतह भी शामिल है. अगर आपने onInterceptTouchEvent(), true दिखाता है, MotionEvent इसे रोका जाता है. इसका मतलब है कि इसे बच्चे के पास नहीं भेजा जाता, बल्कि onTouchEvent() पैरंट तरीके से.

onInterceptTouchEvent() तरीका, माता-पिता को टच इवेंट देखने का मौका देता है इससे पहले कि ये उसके बच्चे हों. अगर आपको onInterceptTouchEvent() से true वापस करना है, तो जो चाइल्ड व्यू पहले टच इवेंट को मैनेज कर रहा था, उसे ACTION_CANCEL, और उस समय के बाद के इवेंट माता-पिता के onTouchEvent() तरीके पर भेजे जाते हैं सामान्य हैंडलिंग के लिए. onInterceptTouchEvent(), false और अपने सामान्य टारगेट के हिसाब से व्यू हैरारकी (व्यू और व्यू ग्रुप के लेआउट का क्रम) तय करने के बाद, इवेंट की जासूसी की जाती है, जो अपने onTouchEvent() के साथ इवेंट.

नीचे दिए गए स्निपेट में, MyViewGroup क्लास ViewGroup का दायरा बढ़ाती है. MyViewGroup में एक से ज़्यादा चाइल्ड व्यू शामिल हैं. अगर बच्चे के किसी व्यू पर अपनी उंगली रखकर उसे खींचकर छोड़ा जाता है हॉरिज़ॉन्टल तौर पर, चाइल्ड व्यू को अब टच इवेंट नहीं मिलेंगे और MyViewGroup, टच को हैंडल करेगा इवेंट के कॉन्टेंट को स्क्रोल करके देखें. हालांकि, अगर आपने चाइल्ड व्यू में बटन पर टैप किया है या बच्चे को स्क्रोल किया है व्यू वर्टिकल तौर पर दिखते हैं, तो माता-पिता उन टच इवेंट को नहीं रोक पाते, क्योंकि बच्चे को टारगेट. ऐसे मामलों में, onInterceptTouchEvent(), false दिखाता है और MyViewGroup क्लास के onTouchEvent() पर कॉल नहीं आया है.

Kotlin

class MyViewGroup @JvmOverloads constructor(
        context: Context,
        private val mTouchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
) : ViewGroup(context) {
    ...
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        // This method only determines whether you want to intercept the motion.
        // If this method returns true, onTouchEvent is called and you can do
        // the actual scrolling there.
        return when (ev.actionMasked) {
            // Always handle the case of the touch gesture being complete.
            MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
                // Release the scroll.
                mIsScrolling = false
                false // Don't intercept the touch event. Let the child handle it.
            }
            MotionEvent.ACTION_MOVE -> {
                if (mIsScrolling) {
                    // You're currently scrolling, so intercept the touch event.
                    true
                } else {

                    // If the user drags their finger horizontally more than the
                    // touch slop, start the scroll.

                    // Left as an exercise for the reader.
                    val xDiff: Int = calculateDistanceX(ev)

                    // Touch slop is calculated using ViewConfiguration constants.
                    if (xDiff > mTouchSlop) {
                        // Start scrolling!
                        mIsScrolling = true
                        true
                    } else {
                        false
                    }
                }
            }
            ...
            else -> {
                // In general, don't intercept touch events. The child view
                // handles them.
                false
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        // Here, you actually handle the touch event. For example, if the action
        // is ACTION_MOVE, scroll this container. This method is only called if
        // the touch event is intercepted in onInterceptTouchEvent.
        ...
    }
}

Java

public class MyViewGroup extends ViewGroup {

    private int mTouchSlop;
    ...
    ViewConfiguration vc = ViewConfiguration.get(view.getContext());
    mTouchSlop = vc.getScaledTouchSlop();
    ...
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // This method only determines whether you want to intercept the motion.
        // If this method returns true, onTouchEvent is called and you can 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; // Don't intercept touch event. Let the child handle it.
        }

        switch (action) {
            case MotionEvent.ACTION_MOVE: {
                if (mIsScrolling) {
                    // You're currently scrolling, so intercept the touch event.
                    return true;
                }

                // If the user drags their finger horizontally more than the
                // touch slop, start the scroll.

                // Left as an exercise for the reader.
                final int xDiff = calculateDistanceX(ev);

                // Touch slop is calculated using ViewConfiguration constants.
                if (xDiff > mTouchSlop) {
                    // Start scrolling.
                    mIsScrolling = true;
                    return true;
                }
                break;
            }
            ...
        }

        // In general, don't intercept touch events. The child view handles them.
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Here, you actually handle the touch event. For example, if the
        // action is ACTION_MOVE, scroll this container. This method is only
        // called if the touch event is intercepted in onInterceptTouchEvent.
        ...
    }
}

ध्यान दें कि ViewGroup requestDisallowInterceptTouchEvent() तरीका. ViewGroup इस तरीके का इस्तेमाल तब करता है, जब बच्चा माता-पिता या अभिभावक की अनुमति नहीं चाहता ऐन्सेस्टर, onInterceptTouchEvent() के साथ टच इवेंट को इंटरसेप्ट करेंगे.

ACTION_OUTSIDE इवेंट प्रोसेस करें

अगर ViewGroup को MotionEvent ACTION_OUTSIDE, यह इवेंट, डिफ़ॉल्ट रूप से अपने बच्चों को नहीं भेजा जाता है. इसकी मदद से MotionEvent को प्रोसेस करने के लिए ACTION_OUTSIDE, या तो बदलें dispatchTouchEvent(MotionEvent event) सही View पर भेजने के लिए या इसे प्रासंगिक तरीके से Window.Callback—इसके लिए उदाहरण के लिए, Activity.

Viewकॉन्फ़िगरेशन कॉन्सटेंट इस्तेमाल करें

पिछला स्निपेट किसी वैरिएबल को शुरू करने के लिए, मौजूदा ViewConfiguration का इस्तेमाल करता है mTouchSlop नाम का इस्तेमाल करें. ऐक्सेस करने के लिए, ViewConfiguration क्लास का इस्तेमाल किया जा सकता है Android सिस्टम में इस्तेमाल की गई सामान्य दूरी, स्पीड, और समय.

"टच स्लोप" से पता चलता है कि हाथ के जेस्चर से पहले उपयोगकर्ता का टच कर सकता है कि वह कितनी दूरी पिक्सल में है जिसे स्क्रोल किया जा सकता है. आम तौर पर, टच स्लोप का इस्तेमाल तब किया जाता है, जब उपयोगकर्ता कोई अन्य टच ऑपरेशन कर रहा हो, जैसे स्क्रीन पर मौजूद तत्वों को छूना.

आम तौर पर इस्तेमाल किए जाने वाले दो अन्य ViewConfiguration तरीके हैं getScaledMinimumFlingVelocity() और getScaledMaximumFlingVelocity(). ये तरीके कम से कम और ज़्यादा से ज़्यादा वेलोसिटी देते हैं, ताकि फ़्लिंग मेज़र की जा सके पिक्सल प्रति सेकंड में दिखाया जाता है. उदाहरण के लिए:

Kotlin

private val vc: ViewConfiguration = ViewConfiguration.get(context)
private val mSlop: Int = vc.scaledTouchSlop
private val mMinFlingVelocity: Int = vc.scaledMinimumFlingVelocity
private val mMaxFlingVelocity: Int = vc.scaledMaximumFlingVelocity
...
MotionEvent.ACTION_MOVE -> {
    ...
    val deltaX: Float = motionEvent.rawX - mDownX
    if (Math.abs(deltaX) > mSlop) {
        // A swipe occurs, do something.
    }
    return false
}
...
MotionEvent.ACTION_UP -> {
    ...
    if (velocityX in mMinFlingVelocity..mMaxFlingVelocity && velocityY < velocityX) {
        // The criteria are satisfied, do something.
    }
}

Java

ViewConfiguration vc = ViewConfiguration.get(view.getContext());
private int mSlop = vc.getScaledTouchSlop();
private int mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
private int mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
...
case MotionEvent.ACTION_MOVE: {
    ...
    float deltaX = motionEvent.getRawX() - mDownX;
    if (Math.abs(deltaX) > mSlop) {
        // A swipe occurs, do something.
    }
...
case MotionEvent.ACTION_UP: {
    ...
    } if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity
            && velocityY < velocityX) {
        // The criteria are satisfied, do something.
    }
}

चाइल्ड व्यू के टच किए जा सकने वाले एरिया को बढ़ाएं

Android, TouchDelegate क्लास का इस्तेमाल करना है माता-पिता बच्चे के देखने के दायरे से बाहर के हिस्से को छू सकते हैं. यह तब काम आता है, जब बच्चे का साइज़ छोटा हो, लेकिन उसे ज़्यादा सेंसर की ज़रूरत हो. आप इसका भी इस्तेमाल कर सकते हैं बच्चे के टच वाले हिस्से को छोटा करने का तरीका.

नीचे दिए गए उदाहरण में, ImageButton _delegate है view_—यानी, वह बच्चा जिसका टच एरिया पैरंट होता है. लेआउट फ़ाइल यहां दी गई है:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/parent_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity" >

     <ImageButton android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@null"
          android:src="@drawable/icon" />
</RelativeLayout>

नीचे दिया गया स्निपेट इन टास्क को पूरा करता है:

  • माता-पिता को व्यू मिलता है और Runnable पोस्ट करता है पर क्लिक करें. इससे यह पक्का हो जाता है कि माता-पिता, getHitRect() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है तरीका. getHitRect() तरीके को बच्चे का हिट रेक्टैंगल मिलता है (या छुई जाने वाली जगह) को माता-पिता के निर्देशांक में देखा जा सकता है.
  • ImageButton चाइल्ड व्यू को ढूंढता है और getHitRect() को कॉल करता है बच्चे के छुए जा सकने वाले हिस्से का दायरा.
  • ImageButton चाइल्ड व्यू के हिट रेक्टैंगल की सीमाओं को बढ़ाता है.
  • विस्तारित हिट आयत के पास से एक TouchDelegate पार करता है और पैरामीटर के तौर पर ImageButton चाइल्ड व्यू.
  • पैरंट व्यू पर TouchDelegate को सेट करता है, ताकि टच डेलिगेट को टच कर सके सीमाएं बच्चे तक पहुंचती हैं.

ImageButton चाइल्ड व्यू के लिए, टच डेलिगेट के तौर पर इसकी क्षमता है सभी स् पर्श इवेंट प्राप्त करता है. अगर टच इवेंट बच्चे के हिट रेक्टैंगल में होता है, तो पैरंट हैंडलिंग के लिए, बच्चे को टच इवेंट भेजता है.

Kotlin

public class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Post in the parent's message queue to make sure the parent lays out
        // its children before you call getHitRect().
        findViewById<View>(R.id.parent_layout).post {
            // The bounds for the delegate view, which is an ImageButton in this
            // example.
            val delegateArea = Rect()
            val myButton = findViewById<ImageButton>(R.id.button).apply {
                isEnabled = true
                setOnClickListener {
                    Toast.makeText(
                            this@MainActivity,
                            "Touch occurred within ImageButton touch region.",
                            Toast.LENGTH_SHORT
                    ).show()
                }

                // The hit rectangle for the ImageButton.
                getHitRect(delegateArea)
            }

            // Extend the touch area of the ImageButton beyond its bounds on the
            // right and bottom.
            delegateArea.right += 100
            delegateArea.bottom += 100

            // Set the TouchDelegate on the parent view so that touches within
            // the touch delegate bounds are routed to the child.
            (myButton.parent as? View)?.apply {
                // 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 receives
                // motion events.
                touchDelegate = TouchDelegate(delegateArea, myButton)
            }
        }
    }
}

Java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the parent view.
        View parentView = findViewById(R.id.parent_layout);

        parentView.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent lays
            // out its children before you call getHitRect().
            @Override
            public void run() {
                // The bounds for the delegate view, which is an ImageButton in
                // this example.
                Rect delegateArea = new Rect();
                ImageButton myButton = (ImageButton) findViewById(R.id.button);
                myButton.setEnabled(true);
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this,
                                "Touch occurred within ImageButton touch region.",
                                Toast.LENGTH_SHORT).show();
                    }
                });

                // 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 receives
                // motion events.
                TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
                        myButton);

                // Set the TouchDelegate on the parent view so that touches
                // within the touch delegate bounds are routed to the child.
                if (View.class.isInstance(myButton.getParent())) {
                    ((View) myButton.getParent()).setTouchDelegate(touchDelegate);
                }
            }
        });
    }
}