2

I have been creating a Gallery derivative that uses a limited number of Views and, as such, the Adapter needs to be able to populate these Views ahead of time during a scroll or a fling. To do this, I need to get the direction of motion from the onFling(...) and onScroll(...) events.

How can I use the distanceX parameter in onScroll(...) and the velocityX parameter in onFling(...) to determine which way the Gallery is travelling, and therefore which View to prepare next?

Andrew Wyld
  • 7,133
  • 7
  • 54
  • 96
  • 1
    Sooo....what's your question? – Kurtis Nusbaum Nov 15 '11 at 15:32
  • I wanted to post it as a question and answer but I wasn't sure how best to do this, as currently it only lets me post answers after a day. If there are any google people about, though, "why?" seems like a reasonable question :) – Andrew Wyld Nov 15 '11 at 16:02
  • This isn't really the place to do that. This is a place for questions where you don't know the answer. If you've found something cool, you should post it to your blog or something. Then in the future when someone is trying to find an answer, they'll google this question and come across you blog. Or someone will ask the question on here and we'll be able to reference your blog. Stackoverflow isn't really meant to be a platform for you to post information, it's not a forum. It's a place to ask questions and get answers. – Kurtis Nusbaum Nov 15 '11 at 16:05
  • OK, shall I remove this? – Andrew Wyld Nov 15 '11 at 16:25
  • 1
    Yes, but don't remove it from the internet :). Post it somewhere were other people can find it. It is in fact useful information and I'm really glad you want to share it. anddev.org would probably be a good place to post it if you don't have you're own blog. – Kurtis Nusbaum Nov 15 '11 at 16:28
  • Ach, anddev isn't letting me register at the moment. Will sort this out later. – Andrew Wyld Nov 15 '11 at 17:06
  • 1
    Rather than removing this useful post I suggest you to edit it a little bit - make it a question (for example about populating Gallery views during fling), and then reply with an answer where you tell about different signs in onScroll and onFling (if you can, provide some code of Views populating during fling). That's it! Read [this blog entry](http://blog.stackoverflow.com/?p=8636) from Jeff Atwood one of the co-founders of SO: **`it is not merely OK to ask and answer your own question, it is explicitly encouraged.`** – Volo Nov 23 '11 at 13:34
  • Now that this has been up a little longer I think I can do that, so I will a bit later on :) – Andrew Wyld Nov 24 '11 at 15:55
  • Done! ... done. – Andrew Wyld Nov 24 '11 at 16:06

1 Answers1

3

The signs of the velocity parameters in onFling(...) and the distance parameters on onScroll(...) are opposite. In order to correctly determine which direction a Gallery is moving, the code should be as follows:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    //if distanceX is POSITIVE, the Views are travelling left
    //therefore the selection position is INCREASING                    
    return super.onScroll(e1, e2, distanceX*mVelocityFactor, distanceY*mVelocityFactor);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    //if velocityX is NEGATIVE, the Views are travelling left
    //therefore the selection position is DECREASING                    
    return super.onFling(e1, e1, velocityX*mVelocityFactor, velocityY*mVelocityFactor);
}

By the way, mVelocityFactor is just a constant I introduced to make the scroll/fling a little less energetic. I found 0.6 to be quite a nice value—it still feels intuitive to scroll but the flings are less violent.

Andrew Wyld
  • 7,133
  • 7
  • 54
  • 96