我在 ListView 上有一个自定义 Swipe 事件,当我从左到右或从右到左滑动时应该触发该事件。我已经得到了整洁和工作,但有一个问题:当我尝试滚动时自然发生的 Fling 事件现在不再生效(我稍后会解释)。如何手动重新创建 Fling 事件?我正在使用的代码如下:
刷卡类:
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!
}
}
自定义检测器类。这个类主要检查滑动的距离:
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 滚动?我的猜测是我必须替换我标记为“帮助!”的代码。有东西,但我不知道是什么。
请帮忙?