I am using ViewPager (support library). I want to know every time the ViewPager change the visible page, it is scrolling left or right.
Please give me a solution. An
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
private int mCurrentSelectedScreen;
private int mNextSelectedScreen;
private static final float thresholdOffset = 0.5f;
private boolean scrollStarted=true, checkDirection=false;
ArrayList<Integer> comp_ary=new ArrayList<Integer>();
@Override
public void onPageSelected(int arg0) {
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
//Log.e("positionOffsetPixels : "+positionOffsetPixels, "positionOffset : "+positionOffset);
comp_ary.add(positionOffsetPixels);
if (checkDirection) {
if (comp_ary.get(2) < comp_ary.get(comp_ary.size()-1)) {
Log.e("going left", "going left");
} else
if (comp_ary.get(2) > comp_ary.get(comp_ary.size()-1))
{
Log.e("going right", "going right");
}
checkDirection = false;
comp_ary=new ArrayList<Integer>();
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
if (!scrollStarted && arg0 == ViewPager.SCROLL_STATE_SETTLING) {
scrollStarted = true;
checkDirection = true;
} else {
scrollStarted = false;
}
}
});
Same solution as GuilhE with a minor fix to avoid getting false positives when paging left (swiping right) on the first page(no more pages to the left) in the ViewPager. It simply does an additional check to see if the swipe has actually moved at all.
new ViewPager.OnPageChangeListener() {
private static final float thresholdOffset = 0.5f;
private static final int thresholdOffsetPixels = 1;
private boolean scrollStarted, checkDirection;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (checkDirection) {
if (thresholdOffset > positionOffset && positionOffsetPixels > thresholdOffsetPixels) {
Log.i(C.TAG, "going left");
} else {
Log.i(C.TAG, "going right");
}
checkDirection = false;
}
}
@Override
public void onPageSelected(int position) {}
@Override
public void onPageScrollStateChanged(int state) {
if (!scrollStarted && state == ViewPager.SCROLL_STATE_DRAGGING) {
scrollStarted = true;
checkDirection = true;
} else {
scrollStarted = false;
}
}
});
Use the ViewPager.OnPageChangeListener interface. You can use the position argument passed to onPageSelected
and compare it to the previous value to figure out which way the ViewPager
was scrolled.
I solved the issue with this implementation. Hope it helps.
public static final float EPSILON= 0.001f;
@Override
public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
// initial position (positionOffset == 0)
if (positionOffset < EPSILON) {
mIsRight = positionOffset < 0.5;
return;
}
// code here
if (mIsRight) {
} else {
}
}
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == pager.getCurrentItem()){
// Move Right
}
else{
// Move Left
}
}