ViewDrawHelpr 鴻翔大神詳解
http://blog.csdn.net/lmj623565791/article/details/46858663
仿淘寶的DEMO
https://github.com/xmuSistone/android-vertical-slide-view
僅供自己參考學(xué)習(xí), 侵刪。
今早看到github模仿淘寶詳情界面的DEMO,參考學(xué)習(xí)。 具體效果就是 兩個(gè)界面,上拉的時(shí)候?qū)崿F(xiàn)阻尼效果,
DEMO中看到VIewDrawHelpr拖拽工具類,先介紹該工具類的使用。
1.創(chuàng)建實(shí)例
mDragger = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback()
第一個(gè)就是當(dāng)前的ViewGroup,第二個(gè)sensitivity(靈敏度),主要用于設(shè)置touchSlop:傳入越大,mTouchSlop的值就會(huì)越小。第三個(gè)參數(shù)就是Callback。
2.觸摸相關(guān)的方法。
@Override public boolean
onInterceptTouchEvent(MotionEvent event) {
// 是否應(yīng)該攔截當(dāng)前的事件
return mDragger.shouldInterceptTouchEvent(event);
}
@Override public boolean onTouchEvent(MotionEvent event) {
// 處理事件
mDragger.processTouchEvent(event); return true;
}
3.ViewDragHelper.CallCack
new ViewDragHelper.Callback() {
@Override public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override public int clampViewPositionHorizontal(View child, int left, int dx) {
return left;
}
@Override public int clampViewPositionVertical(View child, int top, int dy) {
return top;
}
}
****tryCaptureView如何返回ture則表示可以捕獲該view,你可以根據(jù)傳入的第一個(gè)view參數(shù)決定哪些可以捕獲
所有的Callback方法,看看還有哪些沒用過的:
onViewDragStateChanged
當(dāng)ViewDragHelper狀態(tài)發(fā)生變化時(shí)回調(diào)(IDLE,DRAGGING,SETTING[自動(dòng)滾動(dòng)時(shí)])
onViewPositionChanged
view的坐標(biāo)發(fā)生改變,調(diào)用回調(diào)接口中的onViewPositionChanged方法
當(dāng)captureview的位置發(fā)生改變時(shí)回調(diào) 。參數(shù)left和getleft一致,dx為當(dāng)前的和初始值的差值
在2.3.3版本上。需要方法中調(diào)用 invalidate();
onViewCaptured
當(dāng)captureview被捕獲時(shí)回調(diào)
onViewReleased 已用 松手時(shí)候, 參數(shù)xvel松手時(shí)水平方向的速度
onEdgeTouched
當(dāng)觸摸到邊界時(shí)回調(diào)。
onEdgeLock
true的時(shí)候會(huì)鎖住當(dāng)前的邊界,false則unLock。
onEdgeDragStarted 已用
getOrderedChildIndex
改變同一個(gè)坐標(biāo)(x,y)去尋找captureView位置的方法。(具體在:findTopChildUnder方法中)
getViewHorizontalDragRange 已用 松手后的動(dòng)畫時(shí)長(zhǎng)。必須返回一個(gè)大于0的值,否者無法拖動(dòng)。返回實(shí)際的拖拽范圍即可
getViewVerticalDragRange 已用 這個(gè)用來控制拖拽過程中松手后,自動(dòng)滑行的速度
tryCaptureView 已用
clampViewPositionHorizontal 已用
clampViewPositionVertical 已用
方法的大致的回調(diào)順序:
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
?。?!
1.onFinishInflate 當(dāng)View中所有的子控件均被映射成xml后觸發(fā)
2.computeScroll:主要功能是計(jì)算拖動(dòng)的位移量、更新背景、設(shè)置要顯示的屏幕(setCurrentScreen(mCurrentScreen);)。
重寫computeScroll()的原因
調(diào)用startScroll()是不會(huì)有滾動(dòng)效果的,只有在computeScroll()獲取滾動(dòng)情況,做出滾動(dòng)的響應(yīng)
computeScroll在父控件執(zhí)行drawChild時(shí),會(huì)調(diào)用這個(gè)方法
3.mDrawHelper.smoothSlideViewto();平滑移動(dòng)到哪里
只有·覆寫 computeScroll(){} 才能實(shí)現(xiàn)平滑的滾動(dòng)。
