当前位置:网站首页>Internal interception method of incident conflict
Internal interception method of incident conflict
2022-07-20 01:10:00 【Xiao Liu xuezhuo】
Internal interception means that the parent container does not intercept any events , All events are passed to child elements . If the child element needs this event, it directly consumes , Otherwise, it's left to the parent container for processing . The internal interception method needs to cooperate with requestDisallowInterceptTouchEvent Method can work normally , It's a little complicated to use .
The internal interception law needs to be modified :
1、 Internal sliding control dispatchTouchEvent() Method , Control the interception logic of the parent container ;
2、 External slide control ( Parent container ) Need to rewrite onInterceptTouchEvent() Method , Let the parent container not intercept down event , But intercept move and up event .
Go straight to the code
External slide control HorizontalScrollViewEx2
Rewrote onInterceptTouchEvent(), Not blocked by default down event , But intercept other events . Why not intercept down The event is because if down Events have been intercepted , Then view You cannot get events .
Rewrote onTouchEvent(), This part simply performs sliding related operations , It does not involve the code related to event interception , You can skip .
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
mLastX = x;
mLastY = y;
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
return true;
}
return false;
} else {
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "onTouchEvent action:" + event.getAction());
mVelocityTracker.addMovement(event);
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
break;
}
case MotionEvent.ACTION_MOVE: {
int deltaX = x - mLastX;
int deltaY = y - mLastY;
Log.d(TAG, "move, deltaX:" + deltaX + " deltaY:" + deltaY);
scrollBy(-deltaX, 0);
break;
}
case MotionEvent.ACTION_UP: {
int scrollX = getScrollX();
int scrollToChildIndex = scrollX / mChildWidth;
Log.d(TAG, "current index:" + scrollToChildIndex);
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = mVelocityTracker.getXVelocity();
if (Math.abs(xVelocity) >= 50) {
mChildIndex = xVelocity > 0 ? mChildIndex - 1 : mChildIndex + 1;
} else {
mChildIndex = (scrollX + mChildWidth / 2) / mChildWidth;
}
mChildIndex = Math.max(0, Math.min(mChildIndex, mChildrenSize - 1));
int dx = mChildIndex * mChildWidth - scrollX;
smoothScrollBy(dx, 0);
mVelocityTracker.clear();
Log.d(TAG, "index:" + scrollToChildIndex + " dx:" + dx);
break;
}
default:
break;
}
mLastX = x;
mLastY = y;
return true;
}
Internal sliding control ListViewEx
Rewrote dispatchTouchEvent() Method . As long as the event can be transmitted to ListViewEx, It will take so long to walk to dispatchTouchEvent() Methods the internal .
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
mHorizontalScrollViewEx2.requestDisallowInterceptTouchEvent(true);
break;
}
case MotionEvent.ACTION_MOVE: {
int deltaX = x - mLastX;
int deltaY = y - mLastY;
Log.d(TAG, "dx:" + deltaX + " dy:" + deltaY);
if (Math.abs(deltaX) > Math.abs(deltaY)) {
mHorizontalScrollViewEx2.requestDisallowInterceptTouchEvent(false);
}
break;
}
case MotionEvent.ACTION_UP: {
break;
}
default:
break;
}
mLastX = x;
mLastY = y;
return super.dispatchTouchEvent(event);
}
Let's analyze how the event is transmitted to ListViewEx And how to solve sliding conflicts .
1、 First of all down event , down Events enter HorizontalScrollViewEx2 Of dispatchTouchEvent(), And can walk to onInterceptTouchEvent() in . But in onInterceptTouchEvent() Chinese vs down Events are not intercepted . therefore down The event will be passed to ListViewEx.
2、down The event passed on to ListViewEx, The first thing to enter is ListViewEx Of dispatchTouchEvent() Method .ListViewEx Rewrote dispatchTouchEvent() Method , If it is down Event calls the parent container object to execute requestDisallowInterceptTouchEvent(true), It's critical . Next ListViewEx Normal treatment down event ;
3、down The incident was ListViewEx The consumption . So the first one move After the event , First of all to enter HorizontalScrollViewEx2 Of dispatchTouchEvent(), And because of disallowIntercept It's true , As a result, the interception method will not be called onInterceptTouchEvent(). therefore move It is directly passed to ListViewEx.
4、ListViewEx Got the first one move After the event , The first thing to enter is dispatchTouchEvent(), It's internal to move Made a judgment on the direction of : If it slides up and down , Call directly ListView Default implementation of , namely ListViewEx Consumption ; If it's sliding left and right , Then call the parent container object to execute requestDisallowInterceptTouchEvent(false), And then call ListViewEx Default implementation of , ListViewEx Try to consume .
5、 Then come to the second move event , Also go HorizontalScrollViewEx2 Of dispatchTouchEvent(), But at this time disallowIntercept For false , Be able to execute onInterceptTouchEvent() Method . And its internal interception move event . therefore HorizontalScrollViewEx2 Will distribute a ACTION_CANCLE Event to ListViewEx, also HorizontalScrollViewEx2 Will be able to mFirstTouchTarget Set to null. This can lead to ListViewEx You can no longer accept the events behind the same event sequence . And the events behind the same event sequence are directly HorizontalScrollViewEx2 Of onTouchEvent The consumption , And will no longer execute onInterceptTouchEvent() To determine whether to intercept .
6、 Subsequent events are directly HorizontalScrollViewEx2 Intercept consumption .
边栏推荐
- 企业内部创建YUM仓库
- Transaction mechanism: can redis implement acid attribute?
- Chrome install CRX plug-in
- 如何防止你的 jar 被反编译?
- js创意范围选择拖拽插件
- 软件研发落地实践,要从设计就开始
- QT OpenGL (familiar with glsl syntax)
- "Gewu chain collection" launched the "Fuzhou city image logo digital collection", supported by Tencent cloud technology
- 7个模版任你选择!首页自定义全新升级,轻松一键换肤
- LiveData 还有学习的必要吗?—— Jetpack 系列(2)
猜你喜欢
749. Isolate virus: search simulation questions
Solve the problem of single user over receiving coupons under high concurrency
WhaleDI消息队列稳定性提升实践
conda配置tensorflow1.14环境
OPPO 与欧冠达成全球合作,或推出联名科技产品
字符函数和字符串函数和内存函数
Win11 cannot recognize how ADB is set?
解决:el-input设置show-password属性后,谷歌浏览器自动填充之前输入的用户名&密码等。
[paper sharing] information transmission and analogy learning between impulse neural networks, based on astrocytes
Preparation Notes: opencv learning: reading images, color conversion, image scaling, image filtering
随机推荐
Face contour image extraction in online conference (I) -- Preface
每周招聘|去哪儿网(Qunar)DBA招聘、薪资面议,平台无限、能力至上!
3D激光SLAM:ALOAM---gazebo仿真测试场景搭建
2022爱分析・银行数字化实践报告
企业遇到知识管理困境该怎么办?这里有解决方案!
Redis主从同步与故障切换,有哪些坑?
Series operations of set/multiset container (detailed explanation)
OCR ticket recognition based on OpenCV (1)
pytorch的基本操作和代码
The use and Simulation of memcpy function
“格物链藏”首发“福州城市形象标识数字藏品”,腾讯云技术支持
rotate net的pytorch实现
Tencent cloud helped the second China International Digital Products Expo to innovate new experiences of digital and real integration
手把手教你小程序导出Excel
Chrome install CRX plug-in
在线会议中人脸面部轮廓图像提取(一)——前言
Tensorflow performance tuning
软件研发落地实践,要从设计就开始
Implementation: El table page check data and memory
Solution to high game delay in win11