onFling从不触发,但是onDown用于ListView项

时间:2017-05-30 15:28:31

标签: android gesture gesturedetector

我是Android的初学者,现在几个小时我一直困在一个似乎不应该那么难的问题上。我试图在ListView项目上使用onFling。我在这里找到了几个例子和非常相似的问题,但没有一个答案适用于我的情况。

我有一个customAdapter,我在其中调用我的自定义OnTouchListener。

view.setOnTouchListener(new MyTouchListener(context, position));

然后我将GestureDetector放入MyTouchListener并在其onTouch中调用它。

@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
    gdt.onTouchEvent(motionEvent);
    if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
        //Code for drag and dropping
    }


final GestureDetectorCompat gdt = new GestureDetectorCompat(context, new GestureListener());


private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private class GestureListener extends GestureDetector.SimpleOnGestureListener {



    @Override
    public boolean onDown(MotionEvent event){
        Log.i("gwa", "gwa");
        return true;
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i("bla", "bla");
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Log.i("TouchListener", "swipe to Left");
            return false; // Right to left
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Log.i("TouchListener", "swipe to Right");
            return true; // Left to right
        }

        if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Bottom to top
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Top to bottom
        }
        return true;
    }
}

onDown工作并输出日志。但无论我到目前为止尝试过什么,onFling似乎永远不会被触发。

0 个答案:

没有答案
相关问题