I'm changing slide with the following code:
viewPager.setCurrentItem(index++, true);
But it changes too fast. Is there a way to set manually the animation speed?
I'm changing slide with the following code:
viewPager.setCurrentItem(index++, true);
But it changes too fast. Is there a way to set manually the animation speed?
Better solution is to simply access the private fields by creating the class in the support package. EDIT This is bound to the
MAX_SETTLE_DURATION
of 600ms, set by theViewPager
class.You can, of course, then add a custom attribute so this can be set via XML.
I've wanted to do myself and have achieved a solution (using reflection, however).
I haven't tested it yet but it should work or need minimal modification.Tested on Galaxy Nexus JB 4.2.1. You need to use aViewPagerCustomDuration
in your XML instead ofViewPager
, and then you can do this:ViewPagerCustomDuration.java
:ScrollerCustomDuration.java
:Hope this helps someone!
Here is my code used in Librera Reader
I have found better solution, based on @df778899's answer and the Android ValueAnimator API. It works fine without reflection and is very flexible. Also there is no need for making custom ViewPager and putting it into android.support.v4.view package. Here is an example:
UPDATE:
Just checked if this solution can be used to swipe several pages at once (for example if first page should be showed after the last one). This is slightly modified code to handle specified page count:
After wasting my whole day I found a solution set offscreenPageLimit to total no. of the page.
In order to keep a constant length ViewPager scrolls smooth, setOffScreenLimit(page.length) will keep all the views in memory. However, this poses a problem for any animations that involves calling View.requestLayout function (e.g. any animation that involves making changes to the margin or bounds). It makes them really slow (as per Romain Guy) because the all of the views that's in memory will be invalidated as well. So I tried a few different ways to make things smooth but overriding requestLayout and other invalidate methods will cause many other problems.
A good compromise is to dynamically modify the off screen limit so that most of the scrolls between pages will be very smooth while making sure that all of the in page animations smooth by removing the views when the user. This works really well when you only have 1 or 2 views that will have to make other views off memory.
***Use this when no any solution works because by setting offeset limit u will load all the fragments at the same time