android-viewDragHelper总结

来源:互联网 发布:足球指数分析软件 编辑:程序博客网 时间:2024/05/11 13:47

最近又把医生的《英雄传》重新读了一遍,书中有讲到ViewDragHelper这个知识点,之前在鸿洋的博客中也有读到,今天就结合两位大神的总结做一下自己的总结。

一、定义

ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.

这段话是出自于Google官方的API中关于ViewDraghelper的定义,大概意思是说viewDragHelper是写自定义ViewGroups的工具类,对于用户处理views在它的父容器里面的拖动以及相对应的响应,它提供了丰富有用的操作和状态跟踪的API。而这些操作和状态跟踪的API都封装在ViewDragHelper.Callback这个类中,所以总结的重点就是这个类了。

二、API总结

ViewDragHelper类相关的API:
1、create(ViewGroup forParent, ViewDragHelper.Callback cb)
创建viewDragHelper
2、captureChildView(View childView, int activePointerId)
捕获子视图
3、checkTouchSlop(int directions, int pointerId)
检查移动是否为最小的滑动速度
4、findTopChildUnder(int x, int y)
返回指定位置上的顶部子视图
5、flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop)
解决捕获视图自由滑动的位置
6、getActivePointerId()
获取活动的子视图的id
7、getCapturedView()
获取捕获的视图
8、getEdgeSize()
获取边界的大小
9、getMinVelocity()
获取最小的速度
10、getTouchSlop()
获取最小的滑动速度
11、getViewDragState()
获取视图的拖动状态
12、isCapturedViewUnder(int x, int y)
判断该位置是否为捕获的视图
13、isEdgeTouched(int edges)
判断是否为边界触碰
14、setEdgeTrackingEnabled(int edgeFlags)
设置边界跟踪
15、settleCapturedViewAt(int finalLeft, int finalTop)
设置捕获的视图到指定的位置
16、smoothSlideViewTo(View child, int finalLeft, int finalTop)
滑动侧边栏到指定的位置
17、shouldInterceptTouchEvent(MotionEvent ev)
处理父容器是否拦截事件
18、processTouchEvent(MotionEvent ev)
处理父容器拦截的事件

ViewDragHelper.Callback这个类为我们提供的API:
1、clampViewPositionHorizontal(View child, int left, int dx)
控制横轴的移动距离
2、clampViewPositionVertical(View child, int top, int dy)
控制纵轴的移动距离
3、getViewHorizontalDragRange(View child)
获取视图在横轴移动的距离
4、getViewVerticalDragRange(View child)
获取视图在纵轴的移动距离
5、onEdgeDragStarted(int edgeFlags, int pointerId)
处理当用户触碰边界移动开始的回调
6、onEdgeLock(int edgeFlags)
处理边界被锁定时的回调
7、onEdgeTouched(int edgeFlags, int pointerId)
处理边界被触碰时的回调
8、onViewCaptured(View capturedChild, int activePointerId)
当视图被捕获时的回调
9、onViewDragStateChanged(int state)
当视图的拖动状态改变的时候的回调
10、onViewPositionChanged(View changedView, int left, int top, int dx, int dy)
当捕获的视图位置发生改变的时候的回调
11、onViewReleased(View releasedChild, float xvel, float yvel)
当视图的拖动被释放的时候的回调
12、tryCaptureView(View child, int pointerId)
判断此时的视图是否为想要捕获的视图时会调用
13、getOrderedChildIndex(int index)
获取子视图的Z值

三、处理流程

第一步:既然处理拖动就要处理相关的事件,而处理相关的事件系统就交给了ViewDragHelper的shouldInterceptTouchEvent(ev)方法,如果子视图有消费事件的能力,系统必须结合getViewHorizontalDragRange(View child)和getViewVerticalDragRange(View child)获取的值来判断父容器是否拦截事件,所以我们必须重写该两个方法。

第二步:如果ViewDragHelper拦截了事件,我们就要处理掉这个事件,负责处理这个事件的方法是viewDragHelper.processTouchEvent(event)

第三步:前两步都是处理事件,而接下来就是核心了,首先通过tryCaptureView(View child, int pointerId)方法判断是否为想要捕获的视图

第四步:如果是想要捕获的视图的话,则会调用onViewCaptured(View capturedChild, int activePointerId)方法

第五步:接着会调用onViewDragStateChanged(int state)方法

第六步:接着系统会跟随视图的拖动一直会调用clampViewPositionHorizontal(View child, int left, int dx)、clampViewPositionVertical(View child, int top, int dy)和onViewPositionChanged(View changedView, int left, int top, int dx, int dy)这三个方法

第七步:如果停止视图的拖动则会调用onViewReleased(View releasedChild, float xvel, float yvel),然后调用onViewPositionChanged(View changedView, int left, int top, int dx, int dy),最后调用onViewDragStateChanged(int state)

四、练习Demo

可以参考鸿洋大神的博客(http://blog.csdn.net/lmj623565791/article/details/46858663)

0 0