Android下拉刷新与轮播图滑动冲突解决方案
作者:Funzion 时间:2021-08-12 12:30:40
最近在开发中遇到了这样一个问题,在下拉刷新组件中包含了一个轮播图组件,当左右滑动的图片时很容易触发下拉刷新,如下图所示:
如图中红色箭头所示方向切换轮播图,很容易触发下拉刷新。网上查了很多方法,发现都不能很好的解决,于是自己研究了下。
我选用的第三方控件
1.下拉刷新我选用的是chanven的CommonPullToRefresh(系统自带的SwipeRefreshLayout也应该是一样的道理);
2.轮播图选用的是daimajia的AndroidImageSlider(用ViewPager也是一样的道理)。具体界面自行脑补哈。
解决方案
我们仔细分析一下,我们要解决的实际上就是控件的事件拦截问题。现在的情况是外层的控件已经拦截了斜着滑动的事件,那么我们只要让外层的控件把这个事件分发下去就可以了【在dispatchTouchEvent(MotionEvent ev)方法中处理】,那么问题来了,怎么判断斜着的事件。网上有很多方案,但都不是很完美。我想到了一种,跟大家分享一下,先看图:
方案分析
1.图一中x=y,作为临界条件,这时α刚好等于45°;
2.图二中x<y,α>45°,这时我们判断为上下移动;
3.图三中x>y,α<45°,这时我们判断为左右移动。
那么我们只要判断tan(α)与tan(45)的关系就能判断是左右还是上下移动。我们写一个类继承PtrClassicFrameLayout,下面是关键代码:
public class SubPtrClassicFrameLayout extends PtrClassicFrameLayout {
private float mDownX;
private float mDownY;
public SubPtrClassicFrameLayout(Context context) {
super(context);
}
public SubPtrClassicFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SubPtrClassicFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = ev.getX();
mDownY = ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float moveX = ev.getX();
float moveY = ev.getRawY();
float diffX = Math.abs(moveX - mDownX);
float diffY = Math.abs(moveY - mDownY);
boolean isHorizon = Math.tan(diffY / diffX) < Math.tan(45.0);
if (isHorizon) {
return dispatchTouchEventSupper(ev);
}
break;
}
return super.dispatchTouchEvent(ev);
}
}
我们可以看到,其实很简单,关键就是判断当前位置相对于初始位置的滑动方向。
来源:http://www.jianshu.com/p/a169c36f8083#
标签:Android,滑动冲突
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
VsCode搭建Java开发环境的方法
2023-06-17 13:29:19
![](https://img.aspxhome.com/file/2023/9/130919_0s.png)
C#调用C++DLL传递结构体数组的终极解决方案
2022-05-31 09:54:30
一种类似JAVA线程池的C++线程池实现方法
2021-11-02 21:31:52
![](https://img.aspxhome.com/file/2023/7/79917_0s.png)
Java实现三子棋小游戏
2022-09-12 01:27:20
Java构造器与传值学习总结
2023-03-08 23:18:06
java继承中的构造方法实例解析
2022-07-26 02:56:00
java自旋锁和JVM对锁的优化详解
2022-03-02 07:05:33
![](https://img.aspxhome.com/file/2023/2/131992_0s.png)
Java中保证线程顺序执行的操作代码
2023-05-14 17:36:46
Flutter简洁实用的图片编辑器的实现
2021-10-31 08:30:44
![](https://img.aspxhome.com/file/2023/0/116040_0s.gif)
Java实现发送手机短信语音验证功能代码实例
2023-04-04 19:03:31
详解Java中LinkedHashMap
2022-05-23 16:24:45
![](https://img.aspxhome.com/file/2023/1/95031_0s.png)
Spring中的注解之@Override和@Autowired
2022-08-07 19:56:47
![](https://img.aspxhome.com/file/2023/4/125564_0s.png)
SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI
2022-07-08 07:40:23
![](https://img.aspxhome.com/file/2023/6/61516_0s.jpg)
Java注释代码执行方法解析
2023-09-28 00:16:01
Java常量池详解
2023-05-19 19:36:27
SpringCloud服务注册和发现组件Eureka
2023-10-26 06:07:51
![](https://img.aspxhome.com/file/2023/2/79952_0s.png)
Android开发实现删除联系人通话记录的方法
2022-10-26 16:56:49
Spring Boot 集成Dubbo框架实例
2022-02-03 21:23:27
Java Swing实现扫雷源码
2023-11-10 08:16:20
![](https://img.aspxhome.com/file/2023/9/59069_0s.jpg)
Spring框架基于注解开发CRUD详解
2021-08-04 03:39:03
![](https://img.aspxhome.com/file/2023/7/111447_0s.jpg)