Android droidQuery Swipe Length检测

时间:2013-08-21 15:26:42

标签: android swipe droidquery

我使用droidQuery库来处理使用方法

的滑动事件
$.with(myView).swipe(new Function(...));

(请参阅我之前的帖子here),我想知道他们是否是一种扩展答案的方法,以便检查用户刷卡的时间,并根据下降的时间做出不同的反应时间是。谢谢你的回答!

1 个答案:

答案 0 :(得分:1)

您可以按照所讨论的模型here,在滑动逻辑中添加一些其他代码。从链接代码中,我们有以下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;