ViewDragHelper的通用代码模板如下:
public class DragLayout extends FrameLayout {
private ViewDragHelper viewDragHelper;
public DragLayout(Context context){
super(context);
initHelper();
}
public DragLayout(Context context, AttributeSet attrs){
super(context,attrs);
initHelper();
}
public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initHelper();
}
//初始化ViewDragHelper
private void initHelper(){
viewDragHelper=ViewDragHelper.create(this,callback);
}
//处理callback
ViewDragHelper.Callback callback=new ViewDragHelper.Callback() {
//何时开始触摸
@Override
public boolean tryCaptureView(View child, int pointerId) {
return false;
}
//处理水平滑动
@Override
public int clampViewPositionHorizontal(View child,int left,int dx){
return left;
}
//处理垂直滑动
@Override
public int clampViewPositionVertical(View child,int top,int dy){
return top;
}
//拖动结束后调用
@Override
public void onViewReleased(View releaseChild,float xvel,float yvel){
super.onViewReleased(releaseChild,xvel,yvel);
}
//触摸到View时调用
@Override
public void onViewCaptured(View capturedChild,int activePointerId){
super.onViewCaptured(capturedChild,activePointerId);
}
//触摸状态改变时调用,比如STATE_IDLE(没有拖动)、STATE_DRAGGING(正在拖动)、
//STATE_SETTLING(拖动结束)
@Override
public void onViewDragStateChanged(int state){
super.onViewDragStateChanged(state);
}
//触摸位置改变时调用,常用于滑动时更改scale进行缩放
@Override
public void onViewPositionChanged(View changeView,int left,int top,int dx,int dy){
super.onViewPositionChanged(changeView,left,top,dx,dy);
}
};
//重写拦截事件
@Override
public boolean onInterceptTouchEvent(MotionEvent event){
return viewDragHelper.shouldInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event){
//将触摸事件传递给ViewDragHelper
viewDragHelper.processTouchEvent(event);
return true;
}
//重写computeScroll
@Override
public void computeScroll(){
super.computeScroll();
if(viewDragHelper.continueSettling(true)){//如果需要继续滑动,就刷新界面
ViewCompat.postInvalidateOnAnimation(this);
}
}
}
下面使用ViewDragHelper实现类似QQ的滑动侧边栏,具体代码如下:
public class DragLayout extends FrameLayout {
private ViewDragHelper viewDragHelper;
private View mainView,menuView;
private int menuViewWidth;
public DragLayout(Context context){
super(context);
initHelper();
}
public DragLayout(Context context, AttributeSet attrs){
super(context,attrs);
initHelper();
}
public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initHelper();
}
//初始化ViewDragHelper
private void initHelper(){
viewDragHelper=ViewDragHelper.create(this,callback);
}
//处理callback
ViewDragHelper.Callback callback=new ViewDragHelper.Callback() {
//何时开始触摸
@Override
public boolean tryCaptureView(View child, int pointerId) {
return mainView==child;
}
//处理水平滑动
@Override
public int clampViewPositionHorizontal(View child,int left,int dx){
return left;
}
//处理垂直滑动
@Override
public int clampViewPositionVertical(View child,int top,int dy){
return 0;
}
//拖动结束后调用
@Override
public void onViewReleased(View releaseChild,float xvel,float yvel){
super.onViewReleased(releaseChild,xvel,yvel);
if(mainView.getLeft()<menuViewWidth/2){//关闭菜单
viewDragHelper.smoothSlideViewTo(mainView,0,0);
}else {//打开菜单
viewDragHelper.smoothSlideViewTo(mainView,menuViewWidth,0);
}
ViewCompat.postInvalidateOnAnimation(DragLayout.this);
}
};
//重写拦截事件
@Override
public boolean onInterceptTouchEvent(MotionEvent event){
return viewDragHelper.shouldInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event){
//将触摸事件传递给ViewDragHelper
viewDragHelper.processTouchEvent(event);
return true;
}
//重写computeScroll
@Override
public void computeScroll(){
super.computeScroll();
if(viewDragHelper.continueSettling(true)){
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
public void onFinishInflate(){
super.onFinishInflate();
menuView=getChildAt(0);
mainView=getChildAt(1);
}
@Override
public void onSizeChanged(int w,int h,int oldW,int oldH){
super.onSizeChanged(w,h,oldW,oldH);
menuViewWidth=menuView.getMeasuredWidth();
}
}
activity_main布局如下:
<?xml version="1.0" encoding="utf-8"?>
<com.example.draghelperdemo.DragLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="400dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜单布局"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主页布局"/>
</LinearLayout>
</com.example.draghelperdemo.DragLayout>