androidswipedroidquery

Android droidQuery Swipe Length detection


I am using the droidQuery library to handle swipe events using the method

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

(see my previous post here), and I was wondering if their is a way to expand upon the answer in order to check how long the user is swiping, and react differently based on how long the down-time is. Thanks for any answers!


Solution

  • You can follow the model discussed here, with some additional code in the swipe logic. From off the linked code, we have the following switch statement:

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

    To add a check for the down time, add the following class variables:

    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
    

    Then add this to the DOWN case logic:

    start = new Date();
    

    In each of your swipe cases, you can then add this check:

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

    And finally in your UP case, add:

    start = null;
    

    This will make it so that only swipes that are down for longer than LONG_SWIPE_TIME will be handled by your swipe code. For example, for the RIGHT case, you will have:

        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;