本文只是对ViewDragHelper 进行一个基本描述,想要深学的请绕路。
下面是创建ViewDragHelper 的基本构造:
public class HelpLayout extends LinearLayout {
private ViewDragHelper dragHelper;
public HelpLayout(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* 创建ViewDragHelper实例,创建实例需要3个参数
* 第一个就是当前的ViewGroup
* 第二个sensitivity,主要用于设置touchSlop
* 第三个参数就是Callback,在用户的触摸过程中会回调相关方法
*/
dragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
/**
* @param child
* @param pointerId
* @return tryCaptureView如何返回ture则表示可以捕获该view,你可以根据传入的第一个view参数决定哪些可以捕获
*/
@Override
public boolean tryCaptureView(View child, int pointerId) {
if(child.getId()==R.id.button2){
return false;
}
return true;
}
/**
* @param child
* @param top
* @param dy
* @return 垂直方向上的可移动边界
*/
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
return top;
}
/**
* @param child
* @param left
* @param dx
* @return 水平方向上的可移动边界
*/
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return left;
}
});
}
/**
* @param ev
* @return 通过使用mDragger.shouldInterceptTouchEvent(event)来决定我们是否应该拦截当前的事件。
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return dragHelper.shouldInterceptTouchEvent(ev);
}
/**
* @param event
* @return 通过mDragger.processTouchEvent(event)处理事件。
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
dragHelper.processTouchEvent(event);
return true;
}
}
现在我们可以在使用这个布局,在布局中放入控件就可以随意拖动,但有一点需要注意的是,现在可以随意移动的只有clickable=false的控件,因为如果子View不消耗事件,那么整个手势(DOWN-MOVE*-UP)都是直接进入onTouchEvent,在onTouchEvent的DOWN的时候就确定了captureView。如果消耗事件,那么就会先走onInterceptTouchEvent方法,判断是否可以捕获,而在判断的过程中会去判断另外两个回调的方法:getViewHorizontalDragRange和getViewVerticalDragRange,只有这两个方法返回大于0的值才能正常的捕获。
@Override
public int getViewHorizontalDragRange(View child)
{
return getMeasuredWidth()-child.getMeasuredWidth();
}
@Override
public int getViewVerticalDragRange(View child)
{
return getMeasuredHeight()-child.getMeasuredHeight();
}
方法的返回值应当是该childView横向或者纵向的移动的范围,当前如果只需要一个方向移动,可以只复写一个。
到此,我们列一下所有的Callback方法,看看还有哪些没用过的:
onViewDragStateChanged
当ViewDragHelper状态发生变化时回调(IDLE,DRAGGING,SETTING[自动滚动时])
onViewPositionChanged
当captureview的位置发生改变时回调
onViewCaptured
当captureview被捕获时回调
onViewReleased 已用
onEdgeTouched
当触摸到边界时回调。
onEdgeLock
true的时候会锁住当前的边界,false则unLock。
onEdgeDragStarted 已用
getOrderedChildIndex
改变同一个坐标(x,y)去寻找captureView位置的方法。(具体在:findTopChildUnder方法中)
getViewHorizontalDragRange 已用
getViewVerticalDragRange 已用
tryCaptureView 已用
clampViewPositionHorizontal 已用
clampViewPositionVertical 已用
ok,至此所有的回调方法都有了一定的认识。
总结下,方法的大致的回调顺序:
shouldInterceptTouchEvent:
DOWN:
getOrderedChildIndex(findTopChildUnder)
->onEdgeTouched
MOVE:
getOrderedChildIndex(findTopChildUnder)
->getViewHorizontalDragRange &
getViewVerticalDragRange(checkTouchSlop)(MOVE中可能不止一次)
->clampViewPositionHorizontal&
clampViewPositionVertical
->onEdgeDragStarted
->tryCaptureView
->onViewCaptured
->onViewDragStateChanged
processTouchEvent:
DOWN:
getOrderedChildIndex(findTopChildUnder)
->tryCaptureView
->onViewCaptured
->onViewDragStateChanged
->onEdgeTouched
MOVE:
->STATE==DRAGGING:dragTo
->STATE!=DRAGGING:
onEdgeDragStarted
->getOrderedChildIndex(findTopChildUnder)
->getViewHorizontalDragRange&
getViewVerticalDragRange(checkTouchSlop)
->tryCaptureView
->onViewCaptured
->onViewDragStateChanged
ok,上述是正常情况下大致的流程,当然整个过程可能会存在很多判断不成立的情况。
从上面也可以解释,我们在之前TextView(clickable=false)的情况下,没有编写getViewHorizontalDragRange方法时,是可以移动的。因为直接进入processTouchEvent的DOWN,然后就onViewCaptured、onViewDragStateChanged(进入DRAGGING状态),接下来MOVE就直接dragTo了。
而当子View消耗事件的时候,就需要走shouldInterceptTouchEvent,MOVE的时候经过一系列的判断(getViewHorizontalDragRange,clampViewPositionVertical等),才能够去tryCaptureView。
现在只是先了解一下这个类,以后有需要在深究。
参考自:
http://blog.csdn.net/lmj623565791/article/details/46858663
http://blog.csdn.net/lmj623565791/article/details/47396187