Resolved. See answer marked as correct along with my comments. Can post example code if necessary
I'm using the VelocityViewPager 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...
Attempt 1
I can get the snap action to work correctly by using setCurrentItem
. The caveat is that it only snaps if I call the function on an item before or after the current page, for example setCurrentItem(getCurrentItem()+1)
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.
Does anyone have any advice on how I can call setCurrentItem(getCurrentItem())
and have it actually animate/scroll/snap? The source implies that it should. The r7 code seems to always call the scroll function.
Attempt 2
As another test, I did many fakeDragBy(1)
calls to drag the ViewPager
thinking that it would snap after it reached a certain threshold, but that wasn't the case. What's preventing the ViewPager from snapping?
Below is code to reproduce this, if you're interested. I used the same VelocityViewPager code with the following changes
VelocityViewPager.java
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;
}
....
}
MainActivity.java
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;
}
}
activity_main.xml
<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">
<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"/>
</RelativeLayout>