Android dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent Detailed

本文详细解析了Android中触摸事件的传递过程,包括dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent三个关键方法的作用及交互方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

First processing documentation on these three events:

The first one is: of ViewGroup that dispathTouchEvent (MotionEvent ev): pass the Touch event to the target view ().

The second is: of ViewGroup that onInterceptTouchEvent (MotionEvent ev): ViewGroup defined, used to intercept the transfer of Touch events.

Third: View onTouchEvent (the MotionEvent event): the Touch event handlers.

Translation: the ViewGroup The second ViewGroup onInterceptTouchEvent (MotionEvent ev)method

Implement this method to intercept all touch screen motion events. This allows you to watchevents as they are dispatched to your children, and take ownership of the current gesture at any point.

Implement this method to intercept all touch screen events, allows each node to view the event when the event is distributed to sub-view, and have the current gesture ownership

 

Using this function takes some care, as it has a fairly complicated interaction with   and Using it requires IMPLEMENTING that method as well as this one in the correct way. Events will be received in the Following order:

Use this feature to be very careful, because this method and rather complex, using this method requires the correct implementation of these two methods. The event is received in accordance with the following process:

 

1.You will receive the down event here.

First received down event here

 

2.The down event will be handled either by a child of this view group, or given to your own onTouchEvent () method to handle; this means you should implement onTouchEvent () to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent (), you will not receive any following events in onInterceptTouchEvent () and all touch processing must happen in onTouchEvent () like normal.

A down event can view group sub-control processing, or view group to deal with their own.Mean, if you achieve a onTouchEvent () and returns a true, then you will see the following gestures (rather than continue to look for a parent control to handle this event). Also, if you onTouchEvent () to return a true, onInterceptTouchEvent () will no longer receive any of the following events, all touch screen handling in the onTouchEvent ().

 

3.For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent ().

If all events in this method returns false, the next (containing the until up) will be transmitted to the first and then on to the target onTouchEvent ().

 

4.If you return true from here, you will not receive any following events: the target view will receive the same event but with the action   , and all further events will be delivered to your onTouchEvent () METHOD and no longer appear here.

If this returns a ture, you will not receive any of the next event: the target view will receive the same event, action but , and all subsequent events will be passed to the view onTouchEvent () method to deal with, no longer appear here.

 

 

Parameters parameter
ev The motion event being dispatched down the hierarchy.

ev level motion passed down event

 

Returns the return value

Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent (). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

Returns a true value to action events from child controls through onTouchEvent () method to delivery to the group view. This the target receives a ACTION_CANCEL event, and there is no more information will be passed to here.

 

To talk about the event passed in two ways: 
Tunnel: from the root element in turn passed down until the innermost sub-elements or an element in the middle due to a condition to stop the pass. 
Bubble: in order from the innermost sub-elements pass out until the root element or as a condition to stop the pass in the middle of an element.

 

Following a simple experiment to illustrate the complex rules. View from the bottom up of 3 layers, where LayoutView1 and LayoutView2 LinearLayout MyTextView TextView: corresponding xml layout file is as follows:

  1. <span style="font-size:16px;"><com.touchstudy.LayoutView1 xmlns:android="  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.     <com.touchstudy.LayoutView2  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         android:gravity="center"  
  9.         android:orientation="vertical" >  
  10.         <com.touchstudy.MyTextView  
  11.             android:id="@+id/tv"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:background="#FFFFFF"  
  15.             android:text="AB"  
  16.             android:textColor="#0000FF"  
  17.             android:textSize="40sp"  
  18.             android:textStyle="bold" />  
  19.     </com.touchstudy.LayoutView2>  
  20. </com.touchstudy.LayoutView1></span>  

Main Activity: MainActivity.java

  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.view.Window;  
  7. public class MainActivity extends Activity {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14. }  
  15. </span>  

LayoutView1.java

  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class LayoutView1 extends LinearLayout {  
  10.   
  11.     private final String TAG = "LayoutView1";  
  12.   
  13.     public LayoutView1(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         Log.d(TAG, TAG);  
  16.     }  
  17.   
  18.     @Override  
  19.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  20.         int action = ev.getAction();  
  21.         switch (action) {  
  22.         case MotionEvent.ACTION_DOWN:  
  23.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");  
  24.             break;  
  25.         case MotionEvent.ACTION_MOVE:  
  26.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");  
  27.             break;  
  28.         case MotionEvent.ACTION_UP:  
  29.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");  
  30.             break;  
  31.         case MotionEvent.ACTION_CANCEL:  
  32.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");  
  33.             break;  
  34.         }  
  35.         return false;  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onTouchEvent(MotionEvent ev) {  
  40.         int action = ev.getAction();  
  41.         switch (action) {  
  42.         case MotionEvent.ACTION_DOWN:  
  43.             Log.d(TAG, "onTouchEvent action:ACTION_DOWN");  
  44.             break;  
  45.         case MotionEvent.ACTION_MOVE:  
  46.             Log.d(TAG, "onTouchEvent action:ACTION_MOVE");  
  47.             break;  
  48.         case MotionEvent.ACTION_UP:  
  49.             Log.d(TAG, "onTouchEvent action:ACTION_UP");  
  50.             break;  
  51.         case MotionEvent.ACTION_CANCEL:  
  52.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  53.             break;  
  54.         }  
  55.         return false;  
  56.     }  
  57.   
  58.     @Override  
  59.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  60.         int action = ev.getAction();  
  61.         switch (action) {  
  62.         case MotionEvent.ACTION_DOWN:  
  63.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  64.             break;  
  65.         case MotionEvent.ACTION_MOVE:  
  66.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  67.             break;  
  68.         case MotionEvent.ACTION_UP:  
  69.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  70.             break;  
  71.         case MotionEvent.ACTION_CANCEL:  
  72.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");  
  73.             break;  
  74.         }  
  75.         return false;  
  76.     }  
  77.   
  78. }  
  79. </span>  

LayoutView2.java

  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class LayoutView2 extends LinearLayout {  
  10.   
  11.     private final String TAG = "LayoutView2";  
  12.   
  13.     public LayoutView2(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         Log.d(TAG, TAG);  
  16.     }  
  17.   
  18.     @Override  
  19.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  20.         int action = ev.getAction();  
  21.         switch (action) {  
  22.         case MotionEvent.ACTION_DOWN:  
  23.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");  
  24.             break;  
  25.         case MotionEvent.ACTION_MOVE:  
  26.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");  
  27.             break;  
  28.         case MotionEvent.ACTION_UP:  
  29.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");  
  30.             break;  
  31.         case MotionEvent.ACTION_CANCEL:  
  32.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");  
  33.             break;  
  34.         }  
  35.         return false;  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onTouchEvent(MotionEvent ev) {  
  40.         int action = ev.getAction();  
  41.         switch (action) {  
  42.         case MotionEvent.ACTION_DOWN:  
  43.             Log.d(TAG, "onTouchEvent action:ACTION_DOWN");  
  44.             break;  
  45.         case MotionEvent.ACTION_MOVE:  
  46.             Log.d(TAG, "onTouchEvent action:ACTION_MOVE");  
  47.             break;  
  48.         case MotionEvent.ACTION_UP:  
  49.             Log.d(TAG, "onTouchEvent action:ACTION_UP");  
  50.             break;  
  51.         case MotionEvent.ACTION_CANCEL:  
  52.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  53.             break;  
  54.         }  
  55.         return false;  
  56.     }  
  57.   
  58.     @Override  
  59.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  60.         int action = ev.getAction();  
  61.         switch (action) {  
  62.         case MotionEvent.ACTION_DOWN:  
  63.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  64.             break;  
  65.         case MotionEvent.ACTION_MOVE:  
  66.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  67.             break;  
  68.         case MotionEvent.ACTION_UP:  
  69.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  70.             break;  
  71.         case MotionEvent.ACTION_CANCEL:  
  72.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");  
  73.             break;  
  74.         }  
  75.         return false;  
  76.     }  
  77. }  
  78. </span>  

 

MyTextView.java

  1. <span style="font-size:16px;">package com.touchstudy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.TextView;  
  8.   
  9. public class MyTextView extends TextView {  
  10.     private final String TAG = "MyTextView";  
  11.   
  12.     public MyTextView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.         Log.d(TAG, TAG);  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean onTouchEvent(MotionEvent ev) {  
  19.         int action = ev.getAction();  
  20.         switch (action) {  
  21.         case MotionEvent.ACTION_DOWN:  
  22.             Log.d(TAG, "onTouchEvent action:ACTION_DOWN");  
  23.             break;  
  24.         case MotionEvent.ACTION_MOVE:  
  25.             Log.d(TAG, "onTouchEvent action:ACTION_MOVE");  
  26.             break;  
  27.         case MotionEvent.ACTION_UP:  
  28.             Log.d(TAG, "onTouchEvent action:ACTION_UP");  
  29.             break;  
  30.         case MotionEvent.ACTION_CANCEL:  
  31.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  32.             break;  
  33.         }  
  34.         return false;  
  35.     }  
  36.   
  37. }  
  38. </span>  


Prelude to a total of three the view LayoutView1 LayoutView2 MyTextView. They contain relations LayoutView1> LayoutView2> MyTextView. That LayoutView1, LayoutView2 of ViewGroup that, and is View MyTextView. (ViewGroup is a direct subclass of View) three LayoutView1 have dispatchTouchEvent (), onInterceptTouchEvent (), onTouchEvent () method. LayoutView2 dispatchTouchEvent (), onInterceptTouchEvent (), onTouchEvent () three methods. MyTextView onTouchEvent () method. Then, according to the tunneling event pass, so know LayoutView1 dispatchTouchEvent to () is the inlet of the test method (also this is a first response Touch event). We tested variables: 1. Whether to perform the processing method of the parent class. 2 return value of true or false.

The first part, dispatchTouchEvent () method:

1 dispatchTouchEvent () method, the return value to decide whether to block subsequent events.

The false shielded the subsequent events ACTION_MOVE ACTION_UP.

The true, shielded the subsequent events ACTION_MOVE ACTION_UP.

dispatchTouchEvent () method, whether to perform super.dispatchTouchEvent (ev).

Implementation, to call onInterceptTouchEvent () method onTouchEvent () method.

Does not perform, do not call onInterceptTouchEvent () method onTouchEvent () method.

 

The second part of the analysis onInterceptTouchEvent () method:

onInterceptTouchEvent () method, the return value

true, intercept, not down to a level dispatchTouchEvent () method to pass

false, blocking, the down level dispatchTouchEvent () method to pass

Both cases the method of the parent class has nothing to do whether to perform.

The third part, onTouchEvent () method of analysis:

1.onTouchEvent () method, the return value

The false, shielded the subsequent events ACTION_MOVE ACTION_UP.

true, and not to shield the subsequent events ACTION_MOVE ACTION_UP.

Both cases the method of the parent class has nothing to do whether to perform.

Touch event by dispatchTouchEvent the tunnel passed from the top down. The onTouchEvent a View returns true, the next event (After the ACTION_DOWN the ACTION_UP, and that may be contained in the middle certain ACTION_MOVE of, from ACTION_DOWN to ACTION_UP a continuous event, which is they want, and not know the exact N) will still be passed to the View onTouchEvent returns false, the next event will not be passed to the View, but the implementation of the Parent View's onTouchEvent return false onTouchEvent event whenever a View Next event (if there are any) will stop at this View of Parent View, each up a level, bubble-like way.

 

Touch events are passed through the elements in the process are a View, event handling outermost element is not the view is lowered with Window-related events, when a Touch incident, first call the current activity dispatchTouchEvent function , before the event is passed to the View of the lower element. When dispatchTouchEvent passed down through a View, The View is a ViewGroup to call its onInterceptTouchEvent function, this function indicates whether to intercept Touch event, if the function returns true, the said this ViewGroup intercepted the pass of the event, Touch events are not then passed down to its sub-View, but dealt with it, it will call it onTouchEvent function, in the process of passing ViewGroup intercept events, that is, after all ViewGroup returns false, the event will eventually pass to the innermost View, ships is a widget, of course, also be a ViewGroup (its interior does not contain any element) if the last event is passed to the View (non ViewGroup), then first call this View onTouchListener (if set onTouchEvent (a), If onTouchListener returns false then continue to call View, default true), if the last event is passed to a ViewGroup (no sub-View), will call it onTouchEvent function, the default return false.


If you call a the onTouchEvent function of View when returns true, then the next Touch Event event (the the ACTION_DOWN ACTION_UP and may be included in the middle of a number of ACTION_MOVE, from ACTION_DOWN to ACTION_UP as a continuous event, this is what they want, not know the exact N) will continue to be passed to the View and call it the onTouchEvent function onTouchEvent function can conditionally return a different value if a time in this function returns false then the Touch Event event would not and then passed to the View, and in the Parent View terminated call the Parent View onTouchEvent. If all View onTouchEvent function returns false, then the Touch Event event will be handled by the Activity calling Activity onTouchEvent function.

 

When the when ViewGroup dispatchTouchEvent function is called, will be the first call onInterceptTouchEvent function to determine not intercept events, no interceptions (returns false), it will in turn call ViewGroup the child View dispatchTouchEvent function. For example, a FrameLayout is stacked on three of ViewGroup that, then turn in this FrameLayout dispatchTouchEvent of call the these three ViewGroup dispatchTouchEvent of function, and in these three ViewGroup dispatchTouchEvent of will in turn call their the child View dispatchTouchEvent function until The one View dispatchTouchEvent returns true, said it had dealt with the Touch event, do not need to call this View Slibling Views. These three stacked ViewGroup dispatchTouchEvent of functions such as call, if the first ViewGroup dispatchTouchEvent of function returns true (has consumed the event), then the other two ViewGroup dispatchTouchEvent of will no longer be called. Can customize a ViewGroup subclass to reload his dispatchTouchEvent function it treated Touch event still returns false, then it also calls the other brothers View dispatchTouchEvent function.

标题基于SpringBoot+Vue的学生交流互助平台研究AI更换标题第1章引言介绍学生交流互助平台的研究背景、意义、现状、方法与创新点。1.1研究背景与意义分析学生交流互助平台在当前教育环境下的需求及其重要性。1.2国内外研究现状综述国内外在学生交流互助平台方面的研究进展与实践应用。1.3研究方法与创新点概述本研究采用的方法论、技术路线及预期的创新成果。第2章相关理论阐述SpringBoot与Vue框架的理论基础及在学生交流互助平台中的应用。2.1SpringBoot框架概述介绍SpringBoot框架的核心思想、特点及优势。2.2Vue框架概述阐述Vue框架的基本原理、组件化开发思想及与前端的交互机制。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue在学生交流互助平台中的整合方式及优势。第3章平台需求分析深入分析学生交流互助平台的功能需求、非功能需求及用户体验要求。3.1功能需求分析详细阐述平台的各项功能需求,如用户管理、信息交流、互助学习等。3.2非功能需求分析对平台的性能、安全性、可扩展性等非功能需求进行分析。3.3用户体验要求从用户角度出发,提出平台在易用性、美观性等方面的要求。第4章平台设计与实现具体描述学生交流互助平台的架构设计、功能实现及前后端交互细节。4.1平台架构设计给出平台的整体架构设计,包括前后端分离、微服务架构等思想的应用。4.2功能模块实现详细阐述各个功能模块的实现过程,如用户登录注册、信息发布与查看、在线交流等。4.3前后端交互细节介绍前后端数据交互的方式、接口设计及数据传输过程中的安全问题。第5章平台测试与优化对平台进行全面的测试,发现并解决潜在问题,同时进行优化以提高性能。5.1测试环境与方案介绍测试环境的搭建及所采用的测试方案,包括单元测试、集成测试等。5.2测试结果分析对测试结果进行详细分析,找出问题的根源并
内容概要:本文详细介绍了一个基于灰狼优化算法(GWO)优化的卷积双向长短期记忆神经网络(CNN-BiLSTM)融合注意力机制的多变量多步时间序列预测项目。该项目旨在解决传统时序预测方法难以捕捉非线性、复杂时序依赖关系的问题,通过融合CNN的空间特征提取、BiLSTM的时序建模能力及注意力机制的动态权重调节能力,实现对多变量多步时间序列的精准预测。项目不仅涵盖了数据预处理、模型构建与训练、性能评估,还包括了GUI界面的设计与实现。此外,文章还讨论了模型的部署、应用领域及其未来改进方向。 适合人群:具备一定编程基础,特别是对深度学习、时间序列预测及优化算法有一定了解的研发人员和数据科学家。 使用场景及目标:①用于智能电网负荷预测、金融市场多资产价格预测、环境气象多参数预报、智能制造设备状态监测与预测维护、交通流量预测与智慧交通管理、医疗健康多指标预测等领域;②提升多变量多步时间序列预测精度,优化资源调度和风险管控;③实现自动化超参数优化,降低人工调参成本,提高模型训练效率;④增强模型对复杂时序数据特征的学习能力,促进智能决策支持应用。 阅读建议:此资源不仅提供了详细的代码实现和模型架构解析,还深入探讨了模型优化和实际应用中的挑战与解决方案。因此,在学习过程中,建议结合理论与实践,逐步理解各个模块的功能和实现细节,并尝试在自己的项目中应用这些技术和方法。同时,注意数据预处理的重要性,合理设置模型参数与网络结构,控制多步预测误差传播,防范过拟合,规划计算资源与训练时间,关注模型的可解释性和透明度,以及持续更新与迭代模型,以适应数据分布的变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值