0

I'm using a FragmentActivity with a FragmentPagerAdapter which holds a number of Fragment objects through which I can slide with a horizontal swipe.

In the FragmentActivity is a TextView which displays the current item in the list (looks something like this: "3 of 25"). When I swipe to the right I want to increase that number by one, when I swipe to the left the number has to go down by one.

As long as I just swipe in one direction it works just fine but as soon as I change the swiping direction some numbers are being skipped. When I start at #15 it goes up to 16, then 17 then 18. If I swipe to the left then the number goes from 18 to 15.

What is the proper way to implement such a counter?

Dominik
  • 1,703
  • 6
  • 26
  • 46

1 Answers1

0

Add a onPageChagneListener to your ViewPager (through setOnPageChangeListener) and then use the value passed in to onPageSelected to set your current position. My guess (since you provided no code) is that your are using the position passed to getView on your adapter, but this position actually points at views being built off screen in preparation for scrolls, not the view actually being displayed currently.

Nick Campion
  • 10,479
  • 3
  • 44
  • 58
  • Could you post some code snippets below please? I've added this lines of code to the FragmentActivity (using: implements ViewPager.OnPageChangeListener) but the Toast message is never to be seen: private void setOnPageChangeListener() { Toast.makeText(this, "setOnPageChangeListener", 1000).show(); } – Dominik Feb 23 '12 at 15:00