标签:android
public boolean dispatchTouchEvent (MotionEventev) //分发TouchEvent
默认时,InterceptTouchEvent处理
当返回true时,自己处理
返回false时,onTouchEvent
public booleanon InterceptTouchEvent(MotionEvent ev) //拦截TouchEvent
当返回true时,由他自己的OnTouchEvent处理
返回false时,继续分发
public boolean onTouchEvent(MotionEvent ev) //处理TouchEvent
当onTouchEvent返回true时自己处理
当onTouchEvent返回false,继续传递
默认继续传递
其中view类中有dispatchTouchEvent和onTouchEvent两个方法,ViewGroup继承View,而且还新添了一个onInterceptTouchEvent方法。Activity中也无onInterceptTouchEvent方法,但有另外两种方法。
如图,是默认的传播顺序:
布局如下:
<span style="font-family:Microsoft YaHei;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.example.dispatchtest.MyViewGroup android:id="@+id/myviewgroup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <com.example.dispatchtest.MyView android:id="@+id/myview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.example.dispatchtest.MyViewGroup> </RelativeLayout> </span>
<span style="font-family:Microsoft YaHei;">package com.example.dispatchtest; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; public class MyViewGroup extends LinearLayout{ public MyViewGroup(Context context,AttributeSet attrs) { super(context,attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub System.out.println("MyViewGroup dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub System.out.println("MyViewGroup onTouchEvent"); return super.onTouchEvent(event); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub System.out.println("MyViewGroup onInterceptTouchEvent"); return super.onInterceptTouchEvent(ev); } } </span>
<span style="font-family:Microsoft YaHei;">package com.example.dispatchtest; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class MyView extends ImageView{ public MyView(Context context,AttributeSet attrs) { super(context,attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub System.out.println("MyView dispatchTouchEvent"); return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub System.out.println("MyView onTouchEvent"); return super.onTouchEvent(event); } } </span>
<span style="font-family:Microsoft YaHei;">package com.example.dispatchtest; import android.os.Bundle; import android.app.Activity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyView myView = (MyView) findViewById(R.id.myview); myView.setBackgroundResource(R.drawable.ic_launcher); myView.setOnClickListener(this); findViewById(R.id.myviewgroup).setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub System.out.println("MainActivity dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub System.out.println("MainActivity onTouchEvent"); return super.onTouchEvent(event); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.myviewgroup: Toast.makeText(MainActivity.this, "MyViewGroup", Toast.LENGTH_SHORT).show(); break; case R.id.myview: Toast.makeText(MainActivity.this, "MyView", Toast.LENGTH_SHORT).show(); break; default: break; } } } </span>
事件的分发是从上往下一层一层(Activity——ViewGroup——View)
事件的处理则是从下往上一级一级传递(View——ViewGroup——Activity)
比如将MyView的onTouchEvent返回值改为false,则MyView不处理触摸事件,由MyViewGroup处理,Toast的内容是MyViewGroup。
标签:android
原文地址:http://blog.csdn.net/u010335298/article/details/43408709