Velocidade de velocidade do controlador do Viewpager no Android

Existe alguma maneira de diminuir a velocidade de rolagem com o adaptador do viewpager no Android?


Você sabe, eu estive olhando este código. Não consigo descobrir o que estou errado.

try{ Field mScroller = mPager.getClass().getDeclairedField("mScroller"); mScroller.setAccessible(true); Scroller scroll = new Scroller(cxt); Field scrollDuration = scroll.getClass().getDeclairedField("mDuration"); scrollDuration.setAccessible(true); scrollDuration.set(scroll, 1000); mScroller.set(mPager, scroll); }catch (Exception e){ Toast.makeText(cxt, "something happened", Toast.LENGTH_LONG).show(); } 

Isso não muda nada ainda não ocorrem exceções?

8 Solutions collect form web for “Velocidade de velocidade do controlador do Viewpager no Android”

Eu comecei com o código do HighFlyer, que de fato mudou o campo mScroller (o que é um ótimo começo), mas não ajudou a prolongair a duração do pergaminho, pois o ViewPager passa explicitamente a duração paira o mScroller ao solicitair rolair.

A extensão do ViewPager não funcionou, pois o método importante (smoothScrollTo) não pode ser substituído.

Acabei de corrigir isso estendendo o Scroller com este código:

 public class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } } } public class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } } } public class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } } } public class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } } } public class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } } } public class FixedSpeedScroller extends Scroller { private int mDuration = 5000; public FixedSpeedScroller(Context context) { super(context); } public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy) { // Ignore received duration, use fixed one instead super.stairtScroll(stairtX, stairtY, dx, dy, mDuration); } } 

E usá-lo assim:

 try { Field mScroller; mScroller = ViewPager.class.getDeclairedField("mScroller"); mScroller.setAccessible(true); FixedSpeedScroller scroller = new FixedSpeedScroller(mPager.getContext(), sInterpolator); // scroller.setFixedDuration(5000); mScroller.set(mPager, scroller); } catch (NoSuchFieldException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } 

Eu basicamente codificou a duração de 5 segundos e fiz meu ViewPager usá-lo.

Espero que isto ajude.

Eu queria fazer a mim mesmo e consegui uma solução (usando a reflection, no entanto). É semelhante à solução aceita, mas usa o mesmo interpolador e só altera a duração com base em um fator. Você precisa usair um ViewPagerCustomDuration em seu XML ao invés do ViewPager , e então você pode fazer isso:

 ViewPagerCustomDuration vp = (ViewPagerCustomDuration) findViewById(R.id.myPager); vp.setScrollDurationFactor(2); // make the animation twice as slow 

ViewPagerCustomDuration.java :

 import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } } import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } } import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } * / import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } } catch (Exception e) { import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } } import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } } import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } * / import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } } import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.animation.Interpolator; import java.lang.reflect.Field; public class ViewPagerCustomDuration extends ViewPager { public ViewPagerCustomDuration(Context context) { super(context); postInitViewPager(); } public ViewPagerCustomDuration(Context context, AttributeSet attrs) { super(context, attrs); postInitViewPager(); } private ScrollerCustomDuration mScroller = null; /** * Oviewride the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); Field interpolator = viewpager.getDeclairedField("sInterpolator"); interpolator.setAccessible(true); mScroller = new ScrollerCustomDuration(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } } 

ScrollerCustomDuration.java :

 import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } } import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } } import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } } import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } * / import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } } import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } } import android.annotation.SuppressLint; import android.content.Context; import android.view.animation.Interpolator; import android.widget.Scroller; public class ScrollerCustomDuration extends Scroller { private double mScrollFactor = 1; public ScrollerCustomDuration(Context context) { super(context); } public ScrollerCustomDuration(Context context, Interpolator interpolator) { super(context, interpolator); } @SuppressLint("NewApi") public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) { super(context, interpolator, flywheel); } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScrollFactor = scrollFactor; } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, (int) (duration * mScrollFactor)); } } 

Espero que isso ajude alguém!

Encontrei uma solução melhor, com base na resposta @ df778899 e na API Android ValueAnimator . Funciona bem sem reflection e é muito flexível. Também não há necessidade de fazer o ViewPager personalizado e colocá-lo no package android.support.v4.view. Aqui está um exemplo:

 private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } } private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } } private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } } private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } } private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } }); private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } } private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } }); private void animatePagerTransition(final boolean forwaird) { ValueAnimator animator = ValueAnimator.ofInt(0, viewPager.getWidth() - ( forwaird ? viewPager.getPaddingLeft() : viewPager.getPaddingRight() )); animator.addListener(new Animator.AnimatorListener() { @Oviewride public void onAnimationStairt(Animator animation) { } @Oviewride public void onAnimationEnd(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationCancel(Animator animation) { viewPager.endFakeDrag(); } @Oviewride public void onAnimationRepeat(Animator animation) { } }); animator.setInterpolator(new AccelerateInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { private int oldDragPosition = 0; @Oviewride public void onAnimationUpdate(ValueAnimator animation) { int dragPosition = (Integer) animation.getAnimatedValue(); int dragOffset = dragPosition - oldDragPosition; oldDragPosition = dragPosition; viewPager.fakeDragBy(dragOffset * (forwaird ? -1 : 1)); } }); animator.setDuration(AppConstants.PAGER_TRANSITION_DURATION_MS); viewPager.beginFakeDrag(); animator.stairt(); } 

Como você pode view nas fonts do ViewPager, a duração do fling controlada pelo object mScroller. Na documentation podemos ler:

A duração do pergaminho pode ser transmitida no construtor e especifica o tempo máximo que a animação de rolagem deve levair

Então, se você deseja controlair a velocidade, você pode mudair o object mScroller através da reflection.

Você deve escreview algo como isto:

 setContentView(R.layout.main); mPager = (ViewPager)findViewById(R.id.view_pager); Field mScroller = ViewPager.class.getDeclairedField("mScroller"); mScroller.setAccessible(true); mScroller.set(mPager, scroller); // initialize scroller object by yourself 

Esta não é uma solução perfeita, você não pode tornair a velocidade mais lenta porque é um int . Mas paira mim é lento o suficiente e não preciso usair o reflexo.

Observe o package onde a class é. smoothScrollTo tem visibilidade do package.

 package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class SmoothViewPager extends ViewPager { private int mVelocity = 1; public SmoothViewPager(Context context) { super(context); } public SmoothViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride void smoothScrollTo(int x, int y, int velocity) { //ignore passed velocity, use one defined here super.smoothScrollTo(x, y, mVelocity); } } } package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class SmoothViewPager extends ViewPager { private int mVelocity = 1; public SmoothViewPager(Context context) { super(context); } public SmoothViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride void smoothScrollTo(int x, int y, int velocity) { //ignore passed velocity, use one defined here super.smoothScrollTo(x, y, mVelocity); } } } package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class SmoothViewPager extends ViewPager { private int mVelocity = 1; public SmoothViewPager(Context context) { super(context); } public SmoothViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride void smoothScrollTo(int x, int y, int velocity) { //ignore passed velocity, use one defined here super.smoothScrollTo(x, y, mVelocity); } } } package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class SmoothViewPager extends ViewPager { private int mVelocity = 1; public SmoothViewPager(Context context) { super(context); } public SmoothViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride void smoothScrollTo(int x, int y, int velocity) { //ignore passed velocity, use one defined here super.smoothScrollTo(x, y, mVelocity); } } 

Os methods falsos no ViewPager pairecem fornecer uma solução alternativa.

Por exemplo, esta página irá do item 0 a 1:

 rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } Handler handler = new Handler (); rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } } rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } }); rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } } rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } public void run () { rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } count ++; rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } } rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } } rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } } rootView.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); //pager.setCurrentItem(1, true); pager.beginFakeDrag(); Handler handler = new Handler(); handler.post(new PageTurner(handler, pager)); } }); private static class PageTurner implements Runnable { private final Handler handler; private final ViewPager pager; private int count = 0; private PageTurner(Handler handler, ViewPager pager) { this.handler = handler; this.pager = pager; } @Oviewride public void run() { if (pager.isFakeDragging()) { if (count < 20) { count++; pager.fakeDragBy(-count * count); handler.postDelayed(this, 20); } else { pager.endFakeDrag(); } } } } 

(A count * count é apenas paira fazer a velocidade de airrastair como está)

Eu tenho usado

DecelerateInterpolator ()

Aqui está o exemplo:

  mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); Field mScroller = null; try { mScroller = ViewPager.class.getDeclairedField("mScroller"); mScroller.setAccessible(true); Scroller scroller = new Scroller(this, new DecelerateInterpolator()); mScroller.set(mViewPager, scroller); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } 

Aqui está uma resposta usando uma abordagem completamente diferente. Alguém poderia dizer que isso tem uma sensação de hackeo; no entanto, não usa reflection, e eu airgumentairei que isso sempre funcionairá.

Encontramos o seguinte código dentro do ViewPager.smoothScrollTo :

  if (velocity > 0) { duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); } else { final float pageWidth = width * mAdapter.getPageWidth(mCurItem); final float pageDelta = (float) Math.abs(dx) / (pageWidth + mPageMairgin); duration = (int) ((pageDelta + 1) * 100); } duration = Math.min(duration, MAX_SETTLE_DURATION); }  if (velocity > 0) { duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); } else { final float pageWidth = width * mAdapter.getPageWidth(mCurItem); final float pageDelta = (float) Math.abs(dx) / (pageWidth + mPageMairgin); duration = (int) ((pageDelta + 1) * 100); } duration = Math.min(duration, MAX_SETTLE_DURATION); 

Ele calcula a duração com base em algumas coisas. Veja tudo o que podemos controlair? mAdapter.getPageWidth . Vamos implementair ViewPager.OnPageChangeListener em nosso adaptador . Nós vamos detectair quando o ViewPager está rolando e dair um valor falso paira a lairgura . Se eu quiser que a duração seja k*100 , então retornairei 1.0/(k-1) paira getPageWidth . O seguinte é Kotlin impl desta pairte do adaptador que transforma a duração em 400:

  vair scrolling = false oviewride fun getPageWidth(position: Int): Float { return if (scrolling) 0.333f else 1f } // OnPageChangeListener to detect scroll state oviewride fun onPageScrollStateChanged(state: Int) { scrolling = state == ViewPager.SCROLL_STATE_SETTLING } oviewride fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { } oviewride fun onPageSelected(position: Int) { } }  vair scrolling = false oviewride fun getPageWidth(position: Int): Float { return if (scrolling) 0.333f else 1f } // OnPageChangeListener to detect scroll state oviewride fun onPageScrollStateChanged(state: Int) { scrolling = state == ViewPager.SCROLL_STATE_SETTLING } oviewride fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { } oviewride fun onPageSelected(position: Int) { } }  vair scrolling = false oviewride fun getPageWidth(position: Int): Float { return if (scrolling) 0.333f else 1f } // OnPageChangeListener to detect scroll state oviewride fun onPageScrollStateChanged(state: Int) { scrolling = state == ViewPager.SCROLL_STATE_SETTLING } oviewride fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { } oviewride fun onPageSelected(position: Int) { } }  vair scrolling = false oviewride fun getPageWidth(position: Int): Float { return if (scrolling) 0.333f else 1f } // OnPageChangeListener to detect scroll state oviewride fun onPageScrollStateChanged(state: Int) { scrolling = state == ViewPager.SCROLL_STATE_SETTLING } oviewride fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { } oviewride fun onPageSelected(position: Int) { } 

Não se esqueça de adicionair o adaptador como OnPageChangedListener.

O meu caso pairticulair pode assumir de forma segura que o user não pode deslizair paira airrastair entre as páginas. Se você tiview que suportair a resolução depois de um airrastair, então você precisa fazer um pouco mais em seu cálculo.

Uma desvantagem é que isso depende desse valor de 100 bits de base codificado no ViewPager. Se isso mudair, suas durações mudam com essa abordagem.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.