作者:barryjiang(江桓桓)
背景
在我还维护着直播播放模块的时光,在某个阳光明媚忧伤的午后,突然收到一个诡异的bug提醒,简单明了的写着 横屏下弹幕区域无法滑动,竖屏下正常 这里整个示意图方便理解:
思考过程
复盘这个查bug的过程,大致可分为定位问题范围,定位debug方向,关键节点断点调试,根据异常信息假设,根据正确的假设找根因。
定位问题范围
首先,上来先查看提交日志以及历史构建版本验证,看看是哪个小坏蛋提交的bug呢?一看傻眼,是新版本播放器进行重构时引入的bug,视图层级有了不小的变动:
1.重构前
2.重构后
ps:这里为了简化问题背景聚焦bug本身,不过多详细描述需求背景,其变更以及实现方式选型,可以后续讨论。这里的VerticalViewpager名如其意,就是竖直方向的Viewpager。
虽然可以获知是Viewpager+RecyclerView的滑动冲突问题,但目前就只能止步于此,无法更进一步获知具体是哪行代码哪个文件改动产生的bug了。
定位debug方向
ok,目前我们知道了Viewpager+RecyclerView的滑动冲突问题,那么..... 并没有什么卵用妈蛋这个不查日志就知道了好么┴┴︵╰(‵□′)╯︵┴┴
冷静冷静,方向很关键,一时找不到方向时,先补充下基础知识开拓思路是很有效的。
Viewgroup事件分发机制(简略版)
先简单过一遍基础的事件分发机制用到的方法,欲了解详情这里有个博文还不错,可以配合官方文档服用,Android事件分发机制详解[1]。
一般而言,我们接触的最多是这三个函数:
•boolean dispatchTouchEvent (MotionEvent event) Pass the touch screen motion event down to the target view, or this view if it is the target.
Parameters event MotionEvent: The motion event to be dispatched. Returns boolean True if the event was handled by the view, false otherwise.
•boolean onInterceptTouchEvent (MotionEvent ev) Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Parameters ev MotionEvent: The motion event being dispatched down the hierarchy. Returns boolean 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.
•boolean onTouchEvent (MotionEvent event) Implement this method to handle touch screen motion events.
If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:
Parameters event MotionEvent