android.view.GestureDetector

Detects various gestures and events using the supplied MotionEvents. The OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvents reported via touch (don't use for trackball events). To use this class:


boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. The calculated velocity is supplied along the x and y axis in pixels per second.

Parameters:
e1 The first down motion event that started the fling.
e2 The move motion event that triggered the current onFling.
velocityX The velocity of this fling measured in pixels per second along the x axis.
velocityY The velocity of this fling measured in pixels per second along the y axis.
Returns:
true if the event is consumed, else false

创建GestureDetector时,实现OnGestureListener接口中的回调方法。

GestureDetector gestureDetector = new GestureDetector(getActivity(),
        new GestureDetector.OnGestureListener() {
                                                                                                                                   
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
                                                                                                                                   
    @Override
    public void onShowPress(MotionEvent e) {
                                                                                                                                       
    }
                                                                                                                                   
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }
                                                                                                                                   
    @Override
    public void onLongPress(MotionEvent e) {
                                                                                                                                       
    }
                                                                                                                                   
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Logger.i(TAG, "++onFling++");
        Logger.i(TAG, "e1.getAction():"+e1.getAction()+", e1.getX():"+e1.getX()+", e1.getY():"+e1.getY());
        Logger.i(TAG, "e2.getAction():"+e2.getAction()+", e2.getX():"+e2.getX()+", e2.getY():"+e2.getY());
        Logger.i(TAG, "velocityX:"+velocityX+", velocityY:"+velocityY);
        return true; // 需要返回true
    }
                                                                                                                                   
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }
});

在onTouchEvent方法中调用GestureDetector.onTouchEvent方法。

class OnProgramTouchListener implements View.OnTouchListener {
                                             
    float xDown;
    float xLast;
    float xCurrent;
    float xUp;
    float xDistance;
    int newLeftMargin;
    FrameLayout.LayoutParams params = null;
                                             
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Logger.i(TAG, "++programTouchListener.onTouch++");
        gestureDetector.onTouchEvent(event);

创建OnProgramTouchListener的实例对象并且在onActivityCreated方法中使用。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    programListFragment.getView().setOnTouchListener(programTouchListener);
}
View.OnTouchListener programTouchListener = new OnProgramTouchListener();

在onFling方法中打印出来的日志可以发现:onFling方法在getAction()的值等于MotionEvent.ACTION_UP时被调用。

11-01 14:02:25.057: D/WatchTvFragment(20176): ++programTouchListener.onTouch++
11-01 14:02:25.057: D/WatchTvFragment(20176): event.getAction():2
11-01 14:02:25.057: D/WatchTvFragment(20176): leftMargin:337
11-01 14:02:25.057: D/WatchTvFragment(20176): ++programTouchListener.onTouch++
11-01 14:02:25.057: D/WatchTvFragment(20176): ++onFling++
11-01 14:02:25.057: D/WatchTvFragment(20176): e1.getAction():0, e1.getX():155.7273, e1.getY():503.875
11-01 14:02:25.057: D/WatchTvFragment(20176): e2.getAction():1, e2.getX():206.13757, e2.getY():487.01965
11-01 14:02:25.067: D/WatchTvFragment(20176): velocityX:604.2696, velocityY:-86.63345
11-01 14:02:25.067: D/WatchTvFragment(20176): event.getAction():1

日志中getAction()的可选值有:ACTION_UP(1), ACTION_MOVE(2), ACTION_DOWN(0).