You actually can find direction from using parameters of onPageScrolled
.
Following listener is set to show log message once per direction of net fragment change ( left if left fragments reveals, center if no fragment reveals, right if right fragment reveals).
Class variables, need to keep track
// -1 - left, 0 - center, 1 - right
private int scroll = 0;
// set only on `onPageSelected` use it in `onPageScrolled`
// if currentPage < page - we swipe from left to right
// if currentPage == page - we swipe from right to left or centered
private int currentPage = 0;
// if currentPage < page offset goes from `screen width` to `0`
// as you reveal right fragment.
// if currentPage == page , offset goes from `0` to `screen width`
// as you reveal right fragment
// You can use it to see
//if user continue to reveal next fragment or moves it back
private int currentOffset = 0;
// behaves similar to offset in range `[0..1)`
private float currentScale = 0;
The listener
pager.addOnPageChangeListener(
new OnPageChangeListener(){
@Override
public void onPageScrolled(int page, float scale, int offset){
if(scale != 0f && offset > 10){
if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){
if(scroll != -1){
newImageSet = true;
scroll = -1;
Log.e("scroll", "left");
}
} else if(currentOffset < offset && currentScale < scale && page == currentPage){
if(scroll != 1){
newImageSet = true;
scroll = 1;
Log.e("scroll", "right");
}
}
currentOffset = offset;
currentScale = scale;
}
@Override
public void onPageSelected(int i){
Log.e("scroll","centre");
// we are centerd, reset class variables
currentPage = i;
currentScale = 0;
currentOffset = 0;
scroll = 0;
}
@Override
public void onPageScrollStateChanged(int i){
}
}
);