You are right about using ViewPager.OnPageChangeListener
:
@Override
public void onPageSelected(int arg0) {
// programmatically-initiated
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
// user-initiated touch scroll
}
Alternatively, you can use boolean flags to differentiate between programmatically-initiated smooth scroll and a user-initiated touch scroll
. For example, if you use setCurrentItem(int item)
to programmatically change the page, try:
boolean progChange = false;
....
....
....
progChange = true;
setCurrentItem(somePageId); // Set progChange = true every time
....
....
....
Inside your ViewPager.OnPageChangeListener
:
@Override
public void onPageSelected(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
if (progChange) {
// programmatically-initiated
} else {
// user-initiated touch scroll
}
// Set progChange to false;
progChange = false;
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
wasUserScroll()
method for all to use. :) – Trench