ViewDragHelper_v4的滑动视图帮助类_解释和代码

ViewDragHelper :
//is a utility class for writing custom ViewGroups. to drag and reposition views within their parent ViewGroup. 


// android.support.v4.widget.ViewDragHelper


ViewDragHelper mVD = ViewDragHelper.create(ViewGroup forParent,ViewDragHelper.CallBack cb);
ViewDragHelper mVD = ViewDragHelper.create(ViewGroup forParent,float sensitivity,ViewDragHelper.Callback cb);


//作为一个帮助类.首先,需要进行 触摸和拦截的时间转交.
所有捕获的拖动.包括事件和监听,都需要建立在转交的基础上.


方法:
        abort();
        cancel();//相当于 precessTouchEvent(ev);接收到了 Cancel事件


        boolean     continueSettling(boolean deferCallbacks);


        captureChildView(View childView,int activePointerId);//在 parent 中捕获一个详尽的 childView.
        View getCapturedView();
        settleCapturedViewAt(int finalLeft,int finalTop);//放下捕获的视图.


        boolean checkTouchSlop(int derection,int pointerId);//检查滑动轨迹的方向  DIRECTION_HORIZONTAL, DIRECTION_VERTICAL, DIRECTION_ALL
        boolean checkTouchSlop(int directions);
        int getTouchSlop();
        int getViewDragState();// STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.


        int getActivePointerId();


        boolean continueSettling (boolean deferCallbacks);//滑动扑捉到的 View




        boolean isEdgeTouched(int edges);// EDGE_LEFT, EDGE_TOP, EDGE_RIGHT, EDGE_BOTTOM and EDGE_ALL
        boolean isEdgeTouched(int edges,int pointerId);
        boolean isPointerDown(int pointerId);
        boolean isViewUnder(View view,int x,int y);


        int getEdgeSize();
        float getMinVelocity();


        setMinVelocity(float minVe);
    //☆☆☆☆
        void processTouchEvent(MotionEvent ev);//TouchEvent 事件转交
        boolean shouldInterceptTouchEvent(MotionEvent me);
            //Intercept转交 可以作为事件转交


    //☆☆☆☆☆☆    滑动...主动的动作.
        smoothSlideSlideViewTo(View child,int finalleft,int finalTop);


protected
    boolean canScroll(View v,boolean checkV,int dx,int dy,int x,int y);




ViewDragHelper.Callback :
捕获view 后的动作回调..作为执行判断的依据.
// a communication channel with the ViewDragHelper back to the parent view using it
//on*methods are invoked on siginficant events
// Callback 同时可以决定子view 的统治范围和可拖拽性.


        int clampViewPositionHorizontal(View child, int left, int dx);//水平滑动.
            //限定水平滑动的距离...left是坐标.dx为该变量
        clampViewPositionVertical(View child, int top, int dy);//垂直滑动
        getOrderedChildIndex(int index);
        getViewHorizontalDragRange(View child);
        getViewVerticalDragRange(View child);


        boolean tryCaptureView(View child, int pointerId);//    何时开始监控.child当前捕获的view.


    //事件回调
        onEdgeDragStarted(int edgeFlags, int pointerId);
        onEdgeLock(int edgeFlags);
        onEdgeTouched(int edgeFlags, int pointerId);
        onViewCaptured(View capturedChild, int activePointerId);//当用户触摸到 View 后.
        onViewDragStateChanged(int state);//拖拽的状态改变时, idle dragging
        onViewPositionChanged(View changedView, int left, int top, int dx, int dy);//位置改变时回调.  滑动改变 scale等用.
                    //子View移动之后的回调,在该方法中可以获取子VIew移动之后最新的left和top,该方法一般用来做
                    //伴随移动的
        onViewReleased(View releasedChild, float xvel, float yvel);//拖动结束后调用..一般在这里写处理逻辑





事例:
 
<span style="font-family:Microsoft YaHei;font-size:12px;">   /**
    *事件的拦截转交.交给 ViewDragHelper 处理.
    *
    */
    public boolean onInterceptTouchEvent(MotionEvent ev){
        return mVD.shouldInterceptTouchEvent(ev);
    }
    /**
    *事件的转交.交给 ViewDragHelper 处理.
    *
    */
    public boolean onTouchEvent(MotionEvent event){
        mVD.processTouchEvent(event);
        return true;//完全处理.即使在事件回传的时候
    }
    /**
    *使用 ViewDragHelper 内部的 Scroller 进行滑动.但是同样需要 computeScroll 调用.
    *   
    */
    public void computeScroll(){
        if(mVD.continueSettling(true)){             // ViewDragHelper 内部同样是用 Scroller.
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }


    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback(){
        //必须重写的方法
        public boolean tryCaptureView(View child,int pointerId){
            return mMainView==child;
        }


        //返回捕获的控件,移动的距离..事件响应中,也是使用这些值.
        public int clampViewPositionVertical(View child,int top,int dy){
            return top;
        }
        public int clampViewPositionHorizontal(View child,int left,int dx){
            return left;
        }


        //返回捕获控件可以拖动的距离
        @Override
        public int getViewHorizontalDragRange(View child) {


            return int数字;   //有了范围.有了 clampViewPosition** .才会有移动
        }


        //常用.       注意.所有获得的值和控件.也是 tryCaptureView(),clampViewPosition*(),getView*DragRange()等决定的.
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy){


        }




        //常用.           事件回调..释放捕获时.
        public void onViewReleased(View releasedChild,float xvel,float yvel){
            super.onViewReleased(releasedChild,xvel,yvel);
            if(mMainView.getLeft() < 500){
                mVD.smoothSlideSlideViewTo(mMainView,0,0);
                ViewCompat.postInvalidateOnAnimation(DragViewGoup.this);
            }else{
                mVD.smoothSlideSlideViewTo(mMainView,300,0);
                ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
            }
        }
    };</span>

你可能感兴趣的:(android,布局)