4

I am trying to create a Velocity View Pager where in I can scroll through pages like a gallery view. I am experiencing problem while doing scrolling calculations. I am using FragmentStatePagerAdapter for ViewPager. For a normal first time run things are cool with getScrollBy returning the actual scroll position taking 1st element at 0. i.e. if I am at 3rd page, getScrollX returns 2400(800px per page) and so on.

Problem arises when I get the fragment that hosts viewpager after a resume from backstack. This time my 3rd page becomes 0 and my first page becomes -2400 position. Some how when android reinflates the adapter it considers current position as 0. I have tried all sorts using setCurrentItem everywhere I could. Some how I feel computeScroll does not work.

It would be great if any one can suggest how can I make the getScrollX return the position considering start as 0 and not the newly inflated current position.

Else I would have no option to create a new fragment everytime I resume.

Himanshu Virmani
  • 2,450
  • 1
  • 24
  • 34

1 Answers1

1

You know the current position at all times, right? I'd multiply the current position by the width of your ViewPager, using ViewPager.getWidth(). Since the 2400 is in pixels, you can use the width, in pixels from getWidth() to determine the position.

One thing to note is to make sure you use the getWidth() value after the view is inflated and measured, otherwise it will return zero for width.

Flynn81
  • 4,108
  • 29
  • 28