Usage of touchdelegate in Android

Source: Internet
Author: User

Generally, touchdelegate is in the View class.

The following figure shows touchdelegate in the View class:

public class View implements Drawable.Callback, KeyEvent.Callback,        AccessibilityEventSource {// ...    /**     * The delegate to handle touch events that are physically in this view     * but should be handled by another view.     */    private TouchDelegate mTouchDelegate = null;// ...    /**     * Sets the TouchDelegate for this View.     */    public void setTouchDelegate(TouchDelegate delegate) {        mTouchDelegate = delegate;    }    /**     * Gets the TouchDelegate for this View.     */    public TouchDelegate getTouchDelegate() {        return mTouchDelegate;    }// ...    public boolean onTouchEvent(MotionEvent event) {// ...        if (mTouchDelegate != null) {            if (mTouchDelegate.onTouchEvent(event)) {                return true;            }        }// ...}// ...}

Put it here first. Let's take a look at the prompt given by touchdelegate:

/**
* Helper class to handle situations where you want a view to have a larger touch area than its
* Actual view bounds. The view whose touch area is changed is called the delegate view. This
* Class shoshould be used by an ancestor of the Delegate. To use a touchdelegate, first create
* Instance that specifies the bounds that shocould be mapped to the delegate and the delegate
* View itself.
* <P>
* The ancestor shoshould then forward all of its touch events stored ed in its
* {@ Link Android. View. view # ontouchevent (motionevent)} to {@ link # ontouchevent (motionevent )}.
* </P>
*

A large part of e-text is too big, but it is very simple.

I use a picture to show the above meaning:

To put it simply, view2 responds only when we click view2. However, if we want to click the bounds of view1, view2 also responds.

It's that simple...

Therefore, we use the following methods:

        TouchDelegate td = new TouchDelegate(bounds,  view2);        view1.setTouchDelegate(td);

In this case, OK...

So, easy ~~

Of course, you can also derive the touchdelegate class by yourself ..

Post the code here:

// TestActivity.javapublic class TestActivity extends Activity {private LinearLayout mView1, mViewBounds;private Button mView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.a_testactivity);mView1 = (LinearLayout) findViewById(R.id.view1);mViewBounds = (LinearLayout) findViewById(R.id.viewBounds);mView2 = (Button) findViewById(R.id.view2);int v1Top = mView1.getTop();int v1Bottom = mView1.getBottom();ViewGroup.LayoutParams lp1 = mView1.getLayoutParams();ViewGroup.LayoutParams lpBounds = mViewBounds.getLayoutParams();Rect bounds = new Rect();bounds.left = 0;bounds.top = (lpBounds.height - lp1.height)/2;bounds.right = bounds.left + lpBounds.width;bounds.bottom = bounds.top + lpBounds.height;int a=2;int b=a;TouchDelegate td = new TouchDelegate(bounds, mView2);mView1.setTouchDelegate(td);}}
// a_testactivity.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/view1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="300dp"        android:layout_height="200dp"        android:background="@color/white"        android:orientation="horizontal" >        <LinearLayout            android:id="@+id/viewBounds"            android:layout_width="200dp"            android:layout_height="100dp"            android:layout_gravity="center"            android:background="@color/black"            android:orientation="horizontal" >            

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.