前段时间学习了一下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);
}