ViewPager getScrollX returns 0 when fragment resumes / recreates

I am trying to create a Velocity View panel in which I can scroll pages, for example, in the form of a gallery. I'm having trouble scrolling through calculations. I am using FragmentStatePagerAdapter for ViewPager. For a normal launch for the first time, everything is cool when getScrollBy returns the actual scroll position, taking the 1st element at 0. ie, if I'm on the third page, getScrollX returns 2400 (800 pixels per page), etc.

The problem occurs when I get the fragment on which the viewpager is placed after resuming work with backstack. This time my 3rd page will become 0, and the first page will become -2400. For a while, when the android restores the adapter, it considers the current position to be 0. I tried all kinds using setCurrentItem wherever I could. Some, in my opinion, computeScroll do not work.

It would be great if someone could suggest how I can get getScrollX to return a position that treats start as 0, rather than a new, inflated current position.

Else I would not have the opportunity to create a new fragment every time I resume.

+4
source share
1

, ? ViewPager, ViewPager.getWidth(). 2400 , getWidth(), .

, getWidth() , , .

+1

Source: https://habr.com/ru/post/1539572/


All Articles