gpt4 book ai didi

android - 如何让 GestureDetector 正常工作

转载 作者:搜寻专家 更新时间:2023-11-01 08:08:54 25 4
gpt4 key购买 nike

到目前为止,我的 Activity 中有这段代码:

private class SwipeGestureDetector extends SimpleOnGestureListener {
// Swipe properties, you can change it to make the swipe
// longer or shorter and speed
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
float diffAbs = Math.abs(e1.getY() - e2.getY());
float diff = e1.getX() - e2.getX();
Log.d("MainDisplayActivity", "Gesture class is running");
if (diffAbs > SWIPE_MAX_OFF_PATH)
return false;

// Left swipe
if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
MainDisplayActivity.this.onLeftSwipe();

// Right swipe
} else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
MainDisplayActivity.this.onRightSwipe();

}

} catch (Exception e) {
Log.e("YourActivity", "Error on gestures");

}
return false;
}

}//end of SwipeGestureDetector class

//methods called by SwipeGestureDetector when the approrpiate swipes occured
private void onLeftSwipe() {
Toast.makeText(this, "Successfully have the swipe working for left", Toast.LENGTH_SHORT).show();
}

private void onRightSwipe() {
Toast.makeText(this, "Successfully have the swipe working for right", Toast.LENGTH_SHORT).show();
}

我有这个全局的:private GestureDetector gestureDetector;

onCreate 上我有这个,因为我看到人们这样做:

gestureDetector = new GestureDetector(this, new SwipeGestureDetector());
((LinearLayout)findViewById(R.id.parent_main_display)).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});

不太确定自己做错了什么,但滑动时没有任何反应。有什么想法吗?

最佳答案

你应该覆盖下一个

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

onFling 也应该返回 true

关于android - 如何让 GestureDetector 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com