当前位置:操作系统 > 安卓/Android >>

Android中的onInterceptTouchEvent与onTouchEvent

在android中,每一次touch事件基本包含ACTION_DOWN,ACTION_MOVE,ACTION_UP等一系列action。这里需要先明确一下这一次touch事件跟一个ACTION的区别。touch事件是包含ACTION_DOWN,ACTION_MOVE,ACTION_UP等action的,一次touch事件一般会先触发一次ACION_DOWN的action,紧接着是ACTION_MOVE,手指抬起后触发ACTION_UP,那么这一次的touch事件就结束了。当下一个ACTION_DOWN到来时,可能就意味着这是一个新的touch事件了。
当手指触摸到屏幕时,系统就会调用相应View的onTouchEvent,并传入一系列的action。当有多个层级的View时,在父层级允许的情况下,这个action会一直向下传递直到遇到最深层的View。所以touch事件最先调用的是最底层View的onTouchEent,如果View的onTouchEvent接收到某个touch action并作了相应处理,最后有两种返回方式return true和return false;return true会告诉系统当前的View需要处理这次的touch事件,以后的系统发出的ACTION_MOVE,ACTION_UP还是需要继续监听并接收的,而且这次的action已经被处理掉了,父层的View是不可能出发onTouchEvent了。所以每一个action最多只能有一个onTouchEvent接口返回true。如果return false,便会通知系统,当前View不关心这一次的touch事件,此时这个action会传向父级,调用父级View的onTouchEvent。但是这一次的touch事件之后发出的任何action,该View都不会再接受,onTouchEvent在这一次的touch事件中再也不会触发,也就是说一旦View返回false,那么之后的ACTION_MOVE,ACTION_UP等ACTION就不会在传入这个View,但是下一次touch事件的action还是会传进来的。
前面说了底层的View能够接收到这次的事件有一个前提条件:在父层级允许的情况下。假设不改变父层级的dispatch方法,在系统调用底层onTouchEvent之前会先调用父View的onInterceptTouchEvent方法判断,父层View是不是要截获本次touch事件之后的action。如果onInterceptTouchEvent返回了true,那么本次touch事件之后的所有action都不会再向深层的View传递,统统都会传给负层View的onTouchEvent,就是说父层已经截获了这次touch事件,之后的action也不必询问onInterceptTouchEvent,在这次的touch事件之后发出的action时onInterceptTouchEvent不会再次调用,知道下一次touch事件的来临。如果onInterceptTouchEvent返回false,那么本次action将发送给更深层的View,并且之后的每一次action都会询问父层的onInterceptTouchEvent需不需要截获本次touch事件。只有ViewGroup才有onInterceptTouchEvent方法,因为一个普通的View肯定是位于最深层的View,touch事件能够传到这里已经是最后一站了,肯定会调用View的onTouchEvent。
对于底层的View来说,有一种方法可以阻止父层的View截获touch事件,就是调用getParent().requestDisallowInterceptTouchEvent(true);方法。一旦底层View收到touch的action后调用这个方法那么父层View就不会再调用onInterceptTouchEvent了,也无法截获以后的action。
用例子总结一下onInterceptTouchEvent和onTouchEvent的调用顺序:
假设最高层View叫OuterLayout,中间层View叫InnerLayout,最底层View叫MyVIew。调用顺序是这样的(假设各个函数返回的都是false)
OuterLayout.onInterceptTouchEvent->InnerLayout.onInterceptTouchEvent->MyView.onTouchEvent->InnerLayout.onTouchEvent->OuterLayout.onTouchEvent。
layout.xml
[html] 
<com.example.ontouchevent.OuterLayout 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:background="#ffff0000" >  
  
    <com.example.ontouchevent.InnerLayout  
        android:layout_width="match_parent"  
        android:layout_height="200dip"  
        android:background="#ffffff00" >  
  
        <com.example.ontouchevent.MyView  
            android:layout_width="match_parent"  
            android:layout_height="100dip"  
            android:background="#ff00ffff" />  
  
    </com.example.ontouchevent.InnerLayout>  
  
</com.example.ontouchevent.OuterLayout>  
OunterLayout.java
[java] 
package com.example.ontouchevent;  
  
import android.content.Context;  
import android.util.AttributeSet;  
import android.util.Log;  
import android.view.MotionEvent;  
import android.widget.FrameLayout;  
  
public class OuterLayout extends FrameLayout {  
  
    public OuterLayout(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  
  
    public OuterLayout(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        // TODO Auto-generated constructor stub  
    }  
  
    public OuterLayout(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        // TODO Auto-generated constructor stub  
    }  
  
    @Override  
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        boolean res = false;  
        Log.d("TestOnTouchEvent", "OuterLayout onInterceptTouchEvent event:" + ev.getAction() + " returned:" + res);  
        return res;  
    }  
  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        boolean res = true;  
        Log.d("TestOnTouchEvent", "OuterLayout onTouchEvent event:" + event.getAction() + " returned:" + res);  
        return res;  
    }  
  
}  
InnerLayout.java
[java]  
package com.example.ontouchevent;  
  
import android.content.Context;  
import android.util.AttributeSet;  
import android.util.Log;  
import android.view.MotionEvent;  
import android.widget.FrameLayout;  
  
public class InnerLayout extends FrameLayout {  
  
    public InnerLayout(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  
  
    public InnerLayout(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        // TODO Auto-generated constructor stub  
    }  
  
    public InnerLayout(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        // TODO
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,