当前位置:网站首页>Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
2022-07-07 09:52:00 【Time swordsman】
( One ) Overview of incident conflict
Event conflicts usually occur in the parent view Hezi view Combination of , for instance viewpager and listview Combination of . The cause of the event conflict is an event (down,up,move) When we arrive , Sometimes we want to be A view Handle , But it was B view Handle . This leads to what we want to deal with view No events , Don't want to deal with events view Received the incident . And to solve the conflict is to pass viewGroup Of onInterceptTouchEvent() Methods to distribute events reasonably , Let those who want to deal with the event view Received the incident , Don't want to deal with it view Lie flat and do nothing .
( Two ) There is no effect display of event conflict
( Look at the beauty , Write code again ~~)
Get down to business , As shown in the figure above ,demo Is written by a viewpager+listView The combination of , We found that in the case of no event conflict , It should be able to slide left and right , It can also slide up and down . To demonstrate , We inherit from viewpager and Listview,viewPager As a father view, Rewrite its onInterceptTouchEvent Method ,listview As a child view, rewrite dispatchTouchevent.( When needed ),demo The source code of is very simple , I'll give it at the end of the article .
( explain : If you do not override onInterceptTouchEvent and listview Of dispatchTouchevent Method , We use it directly viewpager+listview The combination will find that there will be no event conflict , That's because sdk The conflict has been resolved , You can see viewpager Source code )
Simulate the generation of conflict :
scene 1: When we are in the father view, That is to say viewPager Of onInterceptTouchEvent Method to return true, Intercept the incident , At this time, we will find that we can only respond view pager Slide left and right , And cannot respond listview Slide up and down , The reason is that we are in the father view Directly intercepted the event , Lead to view listview No events , Unable to respond to events .
scene 2: When we are in the father view, That is to say viewPager Of onInterceptTouchEvent Method to return false, Don't intercept events , At this time, we will find ,viewpager Do not slide left or right , It can only slide up and down listview, This is because viewpager Don't intercept events , Distributed the event to the child view Handle , So he can't respond to his left-right sliding event .
( 3、 ... and ) Interview questions : Have you ever encountered sliding conflict ? How did you handle it ?
analysis : At this time, if you have met , Just answer the situation caused by the conflict you encounter , Then say your solution , If you haven't met , Just answer what we mentioned in this article viewpager+listview Scene . Summarize according to the above content , Then there is the answer of how to solve the conflict , At this time, you can answer that there are two ways to solve the conflict :
Method 1 : In the father view Of onInterceptTouchEvent Conflict resolution in , The code is as follows :
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// In the father view Conflict handling in , There is no need for children view Realization dispatchTouchEvent Method
float x = ev.getX();// Record x,y The initial value of the
float y = ev.getY();
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
super.onInterceptTouchEvent(ev);
// You need to call sdk Medium viewpager Medium
//onInterceptTouchEvent Methods to deal with some special problems
// If we don't call, we can't respond to sliding even if we intercept the event
return false;// Can't intercept down event , Intercepted down
// event , You can't receive the following move, and up The incident is over
case MotionEvent.ACTION_MOVE:
float xDiff = Math.abs(x-mLastX);
// When move When the event comes, we will calculate this x Value and last
//x value , You get a difference
float yDiff = Math.abs(y-mLastY);
//y The calculation of the value and x equally
if(xDiff > yDiff){
//x The difference is greater than y The difference between the , Indicates that the current is sliding left and right . At this time, the event should be handed over to viewpager Handle , Intercept the incident
isIntercept = true;
break;
}
if(yDiff > xDiff){
//y Greater than x value , Indicates that it is currently sliding up and down , At this time, the event should be handed over to listview Handle , Don't intercept events
isIntercept = false;
break;
}
// break;
}
mLastX = x;// Update the last x Values and y value
mLastY = y;
return isIntercept;
}
Method 2: In the child view Do sliding conflict processing in ,listview The code in is as follows :
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
// Ask the father view Don't intercept events
break;
case MotionEvent.ACTION_MOVE:
float xDiff = Math.abs(x-mLastX);
float yDiff = Math.abs(y-mLastY);
if(xDiff > yDiff){
// Currently, it slides left and right , You don't need to deal with events , Ask the father view Intercept the incident .
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
default:break;
}
mLastX = x;
mLastY = y;
// Calling the dispatchTouchEvent Handling events
return super.dispatchTouchEvent(ev);
}
viewPager The code in is as follows :
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// In the child view Conflict handling in .
if(ev.getAction() == MotionEvent.ACTION_DOWN){
super.onInterceptTouchEvent(ev);
// Must return false, Otherwise the child view Unable to receive event , There will be an interview question here
return false;
}
// return true, To be a son view Ask the father view When intercepting events , Good interception
return true;
}
In the child view Handle conflicts in and in the parent view The principle of dealing with conflicts is similar , Events are distributed to those who need them through interception and non interception view. When answering, roughly describe the two methods .
And then , The interviewer may ask you : Why not intercept down event , Intercepted down event , hinder move,up Can the event still be received ?
These two problems are actually mutually causal , That is to intercept down event , hinder move,up Events cannot be received . At this time, you can start to put out the source code of the event distribution mechanism you see :
... Some code is omitted
// Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
// If son view There is no call requestDisallowInterceptTouchEvent Method to set the flag , This place will be false, Because in the front down The flag will be reset when the event comes
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);// If intercepted down event , This place will return true; Unable to enter the next event distribution logic , then mFirstTouchTarget Will not be assigned , Cannot walk into the current code logic , Cause the following move and up The event will be directly intercepted
============= Episode ==============
if (!canceled && !intercepted) {
....
// Event distribution processing logic
.....
}
===========================
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
// If not dealt with down At the time of the event ,move and up Events will be intercepted
intercepted = true;
}
... Some code is omitted
Specific event distribution process , See my last blog According to the analysis of popular interview questions Android Event distribution mechanism ( One )
( Four ) summary :
In fact, event distribution mechanism and sliding conflict are not difficult to solve as long as you understand the source code , My suggestion is to read the blog and then go to the source code by yourself , Write demo Simulate every scenario , Then you will find that these principles are all true , It can also help you get the initiative during the interview , Most of the time when we interview, we feel overwhelmed by the interviewer's momentum , In fact, it's all because we don't know much about technology . When we have a thorough understanding of Technology , When you go to the interview, you will find , The interviewer will be abused by you . That feeling is very cool
( 5、 ... and )demo Address
I put this source code in gitee Upper , You have applied for opening permission , It's still being approved , If you urgently need a partner of the source code , You can leave an email , I send it to you in private ~~~~
Event conflict demonstration demo Address
边栏推荐
- Gauss elimination
- 如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
- Write VBA in Excel, connect to Oracle and query the contents in the database
- CDZSC_ 2022 winter vacation personal training match level 21 (1)
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
- 【原创】程序员团队管理的核心是什么?
- Addition, deletion, modification and query of ThinkPHP database
- The industrial chain of consumer Internet is actually very short. It only undertakes the role of docking and matchmaking between upstream and downstream platforms
- Loxodonframework quick start
- CDZSC_2022寒假个人训练赛21级(1)
猜你喜欢

Binary tree high frequency question type

细说Mysql MVCC多版本控制

H5网页播放器EasyPlayer.js如何实现直播视频实时录像?

CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)

How will fashion brands enter the meta universe?

20排位赛3

Arthas simple instructions
![[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision](/img/fd/5e8f74da25d9c5f7bd69dd1cfdcd61.png)
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision

Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)

VSCode+mingw64+cmake
随机推荐
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
H5 web player easyplayer How does JS realize live video real-time recording?
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
Can't connect to MySQL server on '(10060) solution summary
Esp8266 uses TF card and reads and writes data (based on Arduino)
How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
C# 初始化程序时查看初始化到哪里了示例
CDZSC_ 2022 winter vacation personal training match level 21 (1)
JS reverse tutorial second issue - Ape anthropology first question
Applet sliding, clicking and switching simple UI
Unity shader (learn more about vertex fragment shaders)
Dynamics 365Online ApplicationUser创建方式变更
Guys, how can mysql-cdc convert the upsert message to append only
农牧业未来发展蓝图--垂直农业+人造肉
sql 里面使用中文字符判断有问题,哪位遇到过?比如value<>`无`
JS inheritance prototype
Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
[4G/5G/6G专题基础-146]: 6G总体愿景与潜在关键技术白皮书解读-1-总体愿景
Unity shader (basic concept)