0

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!

Community
  • 1
  • 1
superuser
  • 731
  • 10
  • 28

1 Answers1

1

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; 
Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
  • @superuser, that was supposed to just be `start`. I updated my code to reflect that. – Phil Aug 21 '13 at 16:32
  • 1
    thanks so much for all of this help with your library. It is really great. Hopefully it will get even more popular and more information will show up on the web so it will be easier to research. – superuser Aug 21 '13 at 16:43
  • @superuser I am glad you appreciate it! Feel free to share it with others, or include it in answers on SO. I think droidQuery could be very successful if it can gain some more popularity. – Phil Aug 21 '13 at 16:45
  • I have posted another question on how to detect pinching and zooming with your droidQuery library. – superuser Aug 22 '13 at 10:40