gpt4 book ai didi

android - 未检测到滑动

转载 作者:行者123 更新时间:2023-11-29 21:08:06 25 4
gpt4 key购买 nike

在下面发布的代码中,我尝试使用 onDownonFling 检测滑动,但是当我运行应用程序时,没有任何显示。

当我按住屏幕并快速将手指拖过屏幕时,我希望看到来自 onFling 的输出,但我什么也没收到。此外,我希望在按下屏幕时看到来自 onDown 的输出,但也没有显示任何内容。

请告诉我我遗漏了什么或者我误解了 onDownonFling 的功能?!

Java代码:

super.onCreate(savedInstanceState);
setContentView(R.layout.swipe_gesture_activivty);

mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());

mViewGestureDetector = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
};
final TextView view = (TextView) findViewById(R.id.accXLabel);
}

private class MySwipeGestureDetector extends SimpleOnGestureListener implements OnTouchListener {

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_LONG).show();
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}

}

最佳答案

首先,我假设您在 layout.xml 文件中的所有 View 都包含在 framelayout 中。并且您希望在整个 framelayout 中都能检测到您的滑动.如果是这样,请注册您的 framelayoutGestureDetector 类型的对象如下代码所示:

FrameLayout mFL = (FrameLayout) findViewById(R.id.framelayoutID);

mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());
mFL.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
});

关于 simpleOnGestureListener ,这里是如何实现 onFling .它对我有用。:

//define these variables globally:
/* private static final int MIN_DIST = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOC = 200;*/
...
...
...
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("SwipeGesture", "Left Swipe");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("SwipeGesture", "Right Swipe");
}
} catch (Exception e) {
// nothing
}
return false;
}
}

关于android - 未检测到滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858204/

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