title: ViewPager's fakeDragBy() or setCurrentItem() doesn't snap to current item
body: <p>Resolved.  See answer marked as correct along with my comments.  Can post example code if necessary</p>
  
  <hr>
  
  <p>I'm using the <a href="https://stackoverflow.com/a/14969879/2157961">VelocityViewPager</a> to implement a "fling" behavior.  There is a known behavior with it where the page doesn't snap if you drag it by a certain amount.  I'm trying to get around it with little success...</p>
  
  <p><strong>Attempt 1</strong></p>
  
  <p>I can get the snap action to work correctly by using <code>setCurrentItem</code>.  The caveat is that it only snaps if I call the function on an item before or after the current page, for example <code>setCurrentItem(getCurrentItem()+1)</code></p>
  
  <p>So this solution works, but it gives the user a bad user experience if I have to set the current item further than they expect it to.</p>
  
  <p><strong><em>Does anyone have any advice on how I can call <code>setCurrentItem(getCurrentItem())</code> and have it actually animate/scroll/snap?</em></strong>  The source implies that it should.  The <a href="http://grepcode.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/view/ViewPager.java#435" rel="nofollow noreferrer">r7 code</a> seems to always call the scroll function.</p>
  
  <p><strong>Attempt 2</strong></p>
  
  <p>As another test, I did many <code>fakeDragBy(1)</code> calls to drag the <code>ViewPager</code> thinking that it would snap after it reached a certain threshold, but that wasn't the case.  <strong><em>What's preventing the ViewPager from snapping?</em></strong></p>
  
  <p>Below is code to reproduce this, if you're interested.  I used the same <a href="https://stackoverflow.com/a/14969879/2157961">VelocityViewPager</a> code with the following changes</p>
  
  <p><em>VelocityViewPager.java</em></p>
  
  <pre><code>public class VelocityViewPager extends ViewPager implements GestureDetector.OnGestureListener {
  ...
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          // give all the events to the gesture detector. I'm returning true here so the viewpager doesn't
          // get any events at all, I'm sure you could adjust this to make that not true.
          mGestureDetector.onTouchEvent(event);
          if (event.getAction() == MotionEvent.ACTION_UP) {
              setCurrentItem(getCurrentItem(), true);
              Log.d("VelocityViewPager", "Setting to item " + getCurrentItem());
          }
          return true;
      }
  ....
  }
  </code></pre>
  
  <p><em>MainActivity.java</em></p>
  
  <pre><code>package com.example.velocityviewpager;
  
  import android.graphics.Color;
  import android.os.Bundle;
  import android.app.Activity;
  import android.support.v4.view.PagerAdapter;
  import android.view.Gravity;
  import android.view.Menu;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          VelocityViewPager pager = (VelocityViewPager)findViewById(R.id.view);
          PagerAdapter adapter = new PagerAdapter() {
              @Override
              public int getCount() {
                  return 20;
              }
  
              @Override
              public Object instantiateItem(ViewGroup container, int position) {
                  TextView view = new TextView(MainActivity.this);
                  view.setText("Item "+position);
                  view.setGravity(Gravity.CENTER);
                  view.setBackgroundColor(Color.argb(255, position * 10, position * 10, position * 10));
                  view.setTextColor(Color.argb(255, 200, 200, 200));
  
                  container.addView(view);
                  return view;
              }
  
              @Override
              public void destroyItem(ViewGroup container, int position, Object object) {
                  container.removeView((View)object);
              }
  
              @Override
              public boolean isViewFromObject(View view, Object o) {
                  return (view == o);
              }
          };
          pager.setAdapter(adapter);
          adapter.notifyDataSetChanged();
  
          pager.setOffscreenPageLimit(3);
          pager.setCurrentItem(0, true);
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p><em>activity_main.xml</em></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context=".MainActivity"&gt;
  
      &lt;view
              android:layout_width="300dip"
              android:layout_height="300dip"
              class="com.example.velocityviewpager.VelocityViewPager"
              android:id="@+id/view"
              android:layout_alignParentTop="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>