gpt4 book ai didi

android - 确定滑动方向

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

我正在使用 GestureDetector 来调用 onFling()。它似乎在触发我创建的日志消息时正确地检测到我的 flings。我试图确定 throw 的方向,但遇到了问题。传递给 onFling() 方法的两个 MotionEvent 对象的 x 值相同,因此我无法确定方向。例如,我得到:

08-05 16:36:08.679: DEBUG/mView(14616): fling2: 131.0 131.0

当我这样做时:

Log.d("mView", "fling2: " + e1.getX() + " " + e2.getX());

执行 throw 时,我只是水平移动手指,所以这对我来说毫无意义。这里可能出了什么问题?

最佳答案

您可以使用 droidQuery:https://github.com/phil-brown/droidQuery .它将真正简化您的代码并使其易于使用。这是您需要在 Activity onCreate() 中输入的所有内容:

//global variables
private boolean isSwiping = false;
private SwipeDetector.Direction swipeDirection = null;
private View v;//set to the parent layout of the fragments.

//swipe-handling code
$.with(v).swipe(new Function() {
@Override
public void invoke($ droidQuery, Object... params) {
if (params[0] == SwipeDetector.Direction.START)
isSwiping = true;
else if (params[0] == SwipeDetector.Direction.STOP) {
if (isSwiping) {
isSwiping = false;
if (swipeDirection != null) {
switch(swipeDirection) {
case DOWN :
//TODO: Down swipe complete, so do something
break;
case UP :
//TODO: Up swipe complete, so do something
break;
case LEFT :
//TODO: Left swipe complete, so do something
break;
case RIGHT :
//TODO: Right swipe complete, so do something (such as):
day++;
Fragment1 rightFragment = new Fragment1();
Bundle args = new Bundle();
args.putInt("day", day);
rightFragment.setArguments(args);

android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, rightFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
default :
break;
}
}
}
}
else {
swipeDirection = (SwipeDetector.Direction) params[0];
}
}
});

关于android - 确定滑动方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18067336/

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