1

我正在使用一个GestureDetector来电onFling()。它似乎正确地检测到我的投掷,因为它触发了我创建的日志消息。我正在尝试确定投掷的方向,但遇到了问题。MotionEvent传入该方法的两个对象的 x 值相同,onFling()因此我无法确定方向。例如,我得到:

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

当我做:

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

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

4

1 回答 1

2

您可以使用 droidQuery:https ://github.com/phil-brown/droidQuery 。它将真正简化您的代码并使其易于使用。以下是您需要在活动 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];
        }
    }
});
于 2013-08-22T14:45:02.950 回答