前段時間學習了一下ViewDragHelper類,大概的就是經過這個類來控制的上下左右的滑動,方便的不少啊,也是很簡單的代碼,能夠參考我以前的關於ViewDragHelper的博客文章,這裏面主要作的工做就是對觸摸事件的傳遞,須要將觸摸事件傳遞給底層的佈局。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) ev.getX();
startY = (int) ev.getY();
if (bottomState == DEFULT_BOTTOM || bottomState == DEFULT_TOP) {
bottomView.dispatchTouchEvent(ev);
}
topView.dispatchTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
int moveX2 = (int) ev.getX();
int moveY2 = (int) ev.getY();
if (Math.abs((moveY2 - startY)) < 8 ) {
if (getStatusState() == Status.Open && (bottomState == DEFULT_BOTTOM ||
bottomState
==
DEFULT_TOP
)) {
bottomView.dispatchTouchEvent(ev);
}else if (getStatusState() == Status.Close) {
topView.dispatchTouchEvent(ev);
}
}else {
onTouchEvent(ev);
return true ;
}
break;
}
return super.dispatchTouchEvent(ev);
}