onInterceptTouchEvent() 与 onTouch() 事件分析

简介: onInterceptTouchEvent() 与 onTouch() 事件分析

学习笔记:直接上代码,对了在这里强调一点 onTouch() 与 onTouchEvent() 事件不一样。

先看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<com.tinno.intercepttouch.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:text="Hello World!"
       />
</com.tinno.intercepttouch.MyFrameLayout>

MyFrameLayout 是一个自定义View:

public class MyFrameLayout extends FrameLayout  {
    public MyFrameLayout(@NonNull Context context) {
        super(context);
    }
    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                System.out.println("----onInterceptTouchEvent---ACTION_DOWN");
                return true;
           //     break;
            case MotionEvent.ACTION_POINTER_UP:
                System.out.println("----onInterceptTouchEvent---ACTION_POINTER_UP");
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                System.out.println("----onInterceptTouchEvent---ACTION_POINTER_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println("----onInterceptTouchEvent---ACTION_MOVE");
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                System.out.println("----onInterceptTouchEvent---ACTION_UP");
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + event.getActionMasked());
        }
        return false;
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity  {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.tv);
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("----onTouch---ACTION_DOWN");
                    break;
                    case MotionEvent.ACTION_POINTER_UP:
                        System.out.println("----onTouch---ACTION_POINTER_UP");
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        System.out.println("----onTouch---ACTION_POINTER_DOWN");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        System.out.println("----onTouch---ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        System.out.println("----onTouch---ACTION_UP");
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + motionEvent.getActionMasked());
                }
                return true;
            }
        });
    }
}

好了,开始进行分析:


image.png


当 onInterceptTouchEvent 事件返回 true,ViewGroup会将该事件进行拦截,无法向下(View)传递。在  onTouch 中将收不到事件。


当 onTouch  事件返回 true,则表明事件不再向下传递,自己处理,消耗掉,例子:该view的 onClick 事件将会失效。


ViewGroup事件传递总结

image.png

View事件传递总结


image.png



这里需要特别注意的是,onTouch()的执行 先于onClick()。

相关文章
|
28天前
GotFocus和PreviewLeftButtonDown事件
GotFocus和PreviewLeftButtonDown事件
|
7月前
|
搜索推荐 前端开发 UED
关于 beforeinstallprompt 事件
关于 beforeinstallprompt 事件
92 0
|
9月前
|
JavaScript API
StencilJs 学习之事件
其实并没有所谓的 stencil Event,相反 stencil 鼓励使用 DOM event。然而,Stencil 提供了一个 API 来指定组件可以触发的事件,以及组件监听的事件。 这是通过 Event()和 Listen()装饰器实现的。
53 0
|
11月前
|
存储 JSON 前端开发
EventSource 引发的一系列事件 #150
EventSource 引发的一系列事件 #150
203 0
|
API 数据库
9.2领域事件
领域(近似理解为实现某个功能的多个模型)事件可以切断领域模型之间的强依赖关系,事件发布后,由事件的处理者决定如何响应事件,以便于实现事件发布和事件处理的解耦。