0

我正在使用droidQuery库来处理使用该方法的滑动事件

$.with(myView).swipe(new Function(...));

(请参阅我以前的帖子,我想知道他们是否是一种扩展答案的方法,以检查用户滑动多长时间,并根据停机时间的长短做出不同的反应。感谢您的任何回答!

4

1 回答 1

1

您可以遵循此处讨论的模型,并在滑动逻辑中添加一些附加代码。从链接的代码中,我们有以下 switch 语句:

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; 
}

要添加对停机时间的检查,请添加以下类变量:

private Date start;
public static final int LONG_SWIPE_TIME = 400;//this will be the number of milliseconds needed to recognize the event as a swipe

然后将其添加到DOWN案例逻辑中:

start = new Date();

在每个滑动案例中,您都可以添加此检查:

if (start != null && new Date().getTime() - start.getTime() >= LONG_SWIPE_TIME) {
    start = null;
    //handle swipe code here.
}

最后,在您的UP情况下,添加:

start = null;

这样一来,只有滑动LONG_SWIPE_TIME代码可以处理的滑动时间长于滑动代码。例如,对于这种RIGHT情况,您将拥有:

    case RIGHT :
        if (start != null && new Date().getTime() - start.getTime() >= LONG_SWIPE_TIME) {
            start = null;
            //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; 
于 2013-08-21T16:05:05.760 回答