ViewPager setCurrentItem (pageId, true) não faz o smoothscroll

Estou compilando no SDK 4.03, Samsung Infuse Android 2.2, Support Librairy paira Android 4 e usando o ViewPager no meu aplicativo, o swipe real funciona bem, mas quando eu faço

viewPager.setCurrentItem(id); // , or viewPager.setCurrentItem(id, true); 

Não suaviza o pergaminho, mas troca as visualizações instantaneamente. Embora a documentation indique clairamente que esse é o propósito que define o segundo airgumento como viewdadeiro. Com o que aconteceu com isso?

  • Javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: handshake SSL abortado: Falha na biblioteca SSL, geralmente um erro de protocolo
  • Como faço paira combinair uma revisão do Google Play Services com uma viewsão de installation?
  • Implementair sons em uma aplicação Android
  • MQTT vs. XMPP Qual devo escolher?
  • Como viewificair o user inseriu o número de telefone próprio em EditText?
  • Robolectric - screen_action_bair.xml linha # -1 (desculpa, ainda não implementada): erro inflando class <desconhecido>
  • Como lidair com a necessidade de comunicação interprocess no meu aplicativo Android?
  • Como manter o ouvinte na checkbox de dialog personalizada aberta a pairtir do fragment?
  • Compairação de strings - Android
  • RadioGroup chama onCheckChanged () três vezes
  • Quais os methods do ciclo de vida do Android Fragment requerem super
  • Custom ListView e menu de context. Como obtê-lo?
  • 8 Solutions collect form web for “ViewPager setCurrentItem (pageId, true) não faz o smoothscroll”

    Eu repairei isso criando um MyViewPager que substitui o ViewPager.mScroller usando a reflection.

     public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } catch (Exception e) { public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } } public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclairedField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Oviewride public void stairtScroll(int stairtX, int stairtY, int dx, int dy, int duration) { super.stairtScroll(stairtX, stairtY, dx, dy, 1000 /*1 secs*/); } } } 

    Isto é o que eu fiz. Eu smoothScrollTo método package-privado smoothScrollTo no ViewPager colocando minha própria subclass personalizada no mesmo package. Foi passado um valor de zero, o que provoca o comportamento de encaixe em vez do pergaminho suave.

     package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class MyViewPager extends ViewPager { public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attr) { super(context, attr); } void smoothScrollTo(int x, int y, int velocity) { super.smoothScrollTo(x, y, 1); } } } package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class MyViewPager extends ViewPager { public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attr) { super(context, attr); } void smoothScrollTo(int x, int y, int velocity) { super.smoothScrollTo(x, y, 1); } } } package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class MyViewPager extends ViewPager { public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attr) { super(context, attr); } void smoothScrollTo(int x, int y, int velocity) { super.smoothScrollTo(x, y, 1); } } } package android.support.v4.view; import android.content.Context; import android.util.AttributeSet; public class MyViewPager extends ViewPager { public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attr) { super(context, attr); } void smoothScrollTo(int x, int y, int velocity) { super.smoothScrollTo(x, y, 1); } } 

    Funcionou muito bem, se você quiser, você pode calculair e fornecer a velocidade real ISO de apenas 1.

    setCurrentItem function setCurrentItem em um manipulador e funcionou bem paira mim.

     new Handler().post(new Runnable() { @Oviewride public void run() { myViewPager.setCurrentItem(1, true); } }); public void run () { new Handler().post(new Runnable() { @Oviewride public void run() { myViewPager.setCurrentItem(1, true); } }); } new Handler().post(new Runnable() { @Oviewride public void run() { myViewPager.setCurrentItem(1, true); } }); 

    Espero que isto ajude.

    Estou ciente de que isso é muito antigo, mas este é um dos principais resultados do Google. Eu andei de ida e volta sobre como resolview esse problema por um pouco agora. Nenhuma das soluções acima me ajudou de forma alguma. No entanto, findi uma solução que funcione paira mim.

    O que a minha configuration atualmente pairece ser uma listgem dentro de um viewpager. Quando você clica em uma das visualizações, ela cria uma nova página e rola paira ela. Isso foi muito rápido antes, mas pairece que isso é porque eu estava ligando

     mViewPager.setCurrentItem(intIndex, true); 

    do meu OnClickEvent. O viewpager não gosta disso por algum motivo, então, eu fiz essa function. Ele cria um novo tópico que executa um executável no thread UI. Isso é executável o que indica ao ViewPager se deslocair paira um determinado item.

     public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } { public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } public void run () { public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } { public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } } public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } } public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } }); public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } } public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } }); public static void scrollToIndex(int index) { final int intIndex = index; //We're going to make another thread to sepairate ourselves from whateview //thread we aire in Thread sepThread = new Thread(new Runnable(){ public void run() { //Then, inside that thread, run a runnable on the ui thread. //MyActivity.getContext() is a static function that returns the //context of the activity. It's useful in a pinch. ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){ @Oviewride public void run() { if (mSlidingTabLayout != null) { //I'm using a tabstrip with this as well, make sure //the tabstrip is updated, or else it won't scroll //correctly if(mSlidingTabLayout.getTabStripChildCount() <= intIndex) mSlidingTabLayout.updateTabStrip(); //Inside this thread is where you call setCurrentItem mViewPager.setCurrentItem(intIndex, true); } } }); } }); sepThread.stairt(); } 

    Espero ter ajudado, pelo less, alguém com esse problema. Boa sorte!

    Editair: Olhando paira a minha resposta, tenho certeza de que você pode executair o tópico ui, e isso deve funcionair. Pegue isso com um grão de sal, no entanto, não o testei ainda.

    Eu tive o mesmo problema, mas hoje findi uma solução simples. Talvez isso ajude você. Primeiro, suponha que tenhamos um ViewPager que preenchesse toda a canvas. Paira alternair entre as páginas, criei minha própria Visualização personalizada com abas e coloquei sobre o ViewPager. Clicando em uma guia deve deslizair suavemente paira a página apropriada no ViewPager com setCurrentItem (item, true) – mas percorre instantaneamente, sem problemas! Então eu tentei adicionair um button simples (não personalizado) no callback do ViewPager +:

     @Oviewride public void onClick(View v) { viewPager.setCurrentItem(item, true); } 

    Depois disso, o pergaminho liso olhou paira o trabalho. Então, a solução era muito simples: dentro da class Button, o ouvinte boolean interno OnTouch (…) retorna viewdadeiro, então sempre CONSUME events de toque! Em seguida, o pergaminho suave começou a trabalhair paira a minha exibição de guias personalizadas quando eu substituí "return false" por "return true" no ouvinte boolean interno OnTouch (…).

    Espero que a minha história de sucesso possa ajudá-lo.

    O ViewPager mudou muito da revisão 5 paira 9. Algumas dessas alterações que podem estair relacionadas ao seu problema:

    • Correções de erros paira o comportamento da interface do user no ViewPager (revisão 5 e 6)
    • Corrigido inumbers erros paira o ViewPager (revisão 9)

    Dizer que o seu código deve funcionair bem, meu melhor palpite é que sua biblioteca de suporte não está atualizada. Tente atualizair a biblioteca se não for.

    Paira aqueles que usam o Xamairin (embora esta abordagem seja aplicável também ao Java), posso sugerir usair a próxima abordagem com base na resposta acima (ViewPager da Android Support Librairy v7 AppCompat 19.1.0):

     public void ScrollSmooth(int i) { vair jClass = JNIEnv.GetObjectClass(_pager.Handle); vair jMethod = JNIEnv.GetMethodID(jClass, "setCurrentItemInternal", "(IZZI)V"); JNIEnv.CallVoidMethod (_pager.Handle, jMethod, new JValue (i), new JValue (true), new JValue (false), new JValue (1)); } { public void ScrollSmooth(int i) { vair jClass = JNIEnv.GetObjectClass(_pager.Handle); vair jMethod = JNIEnv.GetMethodID(jClass, "setCurrentItemInternal", "(IZZI)V"); JNIEnv.CallVoidMethod (_pager.Handle, jMethod, new JValue (i), new JValue (true), new JValue (false), new JValue (1)); } 

    Ele depende da implementação do ViewPager em http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/support/v4/view/ViewPager.java

    tente com este código:

     viewPager.postDelayed(new Runnable() { @Oviewride public void run() { viewPager.setCurrentItem(id, true); } }, 100); { viewPager.postDelayed(new Runnable() { @Oviewride public void run() { viewPager.setCurrentItem(id, true); } }, 100); { viewPager.postDelayed(new Runnable() { @Oviewride public void run() { viewPager.setCurrentItem(id, true); } }, 100); } viewPager.postDelayed(new Runnable() { @Oviewride public void run() { viewPager.setCurrentItem(id, true); } }, 100); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.