自定义滑动事件干扰默认Fling

时间:2013-06-11 08:10:56

标签: android listview

我在ListView上有一个自定义Swipe事件,当我从左向右或从右向左滑动时,该事件应该触发。我有那么整洁和工作,但有一个问题:当我试图滚动时自然发生的Fling事件不再生效(我稍后会解释)。如何手动重新创建Fling事件?我正在使用的代码如下:

Swipe类:

public class Swipe implements SwipeInterface {

public Swipe() {}

@Override
public void bottom2top(View v) {
    //Help!
}

@Override
public void left2right(View v) {
    //Does logic here
}

@Override
public void right2left(View v) {
    //Does logic here
}

@Override
public void top2bottom(View v) {
    //Help!
}

}

自定义Detector类。该类主要检查滑动的距离:

public class ActivitySwipeDetector implements View.OnTouchListener {

static final String logTag = "ActivitySwipeDetector";
private Swipe swipe;
static final int MIN_DISTANCE = 200;
private float downX, downY, upX, upY;

public ActivitySwipeDetector(Swipe swipe) {
    this.swipe = swipe;
}

public void onRightToLeftSwipe(View v) {
    Log.i(logTag, "RightToLeftSwipe!");
    swipe.right2left(v);
}

public void onLeftToRightSwipe(View v) {
    Log.i(logTag, "LeftToRightSwipe!");
    swipe.left2right(v);
}

public void onTopToBottomSwipe(View v) {
    Log.i(logTag, "onTopToBottomSwipe!");
    swipe.top2bottom(v);
}

public void onBottomToTopSwipe(View v) {
    Log.i(logTag, "onBottomToTopSwipe!");
    swipe.bottom2top(v);
}

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        downX = event.getX();
        downY = event.getY();
        return true;
    }
    case MotionEvent.ACTION_UP: {
        upX = event.getX();
        upY = event.getY();

        float deltaX = downX - upX;
        float deltaY = downY - upY;

        // swipe horizontal?
        if (Math.abs(deltaX) > MIN_DISTANCE) {
            // left or right
            if (deltaX < 0) {
                this.onLeftToRightSwipe(v);
                return true;
            }
            if (deltaX > 0) {
                this.onRightToLeftSwipe(v);
                return true;
            }
        } else {
            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
        }

        // swipe vertical?
        if (Math.abs(deltaY) > MIN_DISTANCE) {
            // top or down
            if (deltaY < 0) {
                this.onTopToBottomSwipe(v);
                return true;
            }
            if (deltaY > 0) {
                this.onBottomToTopSwipe(v);
                return true;
            }
        } else {
            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
        }
    }
    }
    return false;
}

}

最后,这就是我实现自定义侦听器的方法:

Swipe swipe = new Swipe();
ActivitySwipeDetector detector = new ActivitySwipeDetector(swipe);
listview.setOnTouchListener(detector);

同样,它工作得很漂亮,我仍然可以在ListView上实现OnItemClickListener,但由于Swipe类中覆盖了bottom2top和top2bottom方法,它不再执行任何“fling scrolls”。所以问题是如何再次“创建”Fling滚动?我的猜测是我必须替换我标记为“帮助!”的代码。有什么,但我找不到什么。

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

为什么不使用GestureDetector来检测滑动操作。