Andrew Andrew - 1 month ago 15
Android Question

Android ViewPager's onPageScrollStateChanged never gets called

I have extended the ViewPager class and implemented onPageChangeListener.
I use this ViewPager in conjunction with a CirclePageIndicator inside a SherlockFragment. The ViewPager contains ImageViews and TextViews, nothing fancy.

I need to do something below the ViewPager every time the Page is being changed. So, I figured I need to use the onPageScrollStateChanged and verify when the state is IDLE, or something.

The problem is that from the 3 implemented methods (onPageScrollStateChanged, onPageSelected and onPageScrolled), only onPageScrolled gets called (I checked with a log message in each method). Why is that ? Code below.

public class UninterceptableViewPager extends ViewPager implements OnPageChangeListener{

private ArrayList<TwoRowListViewAdapter> mAdaptersList = null;
private ListView mListView = null;
private ProgressBar mProgressBar = null;


public UninterceptableViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
}

public boolean onInterceptTouchEvent(MotionEvent event)
{
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(event);
}


public void setAdaptersList(ArrayList<TwoRowListViewAdapter> adaptersList)
{
mAdaptersList = adaptersList;
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
super.onPageScrolled(position++, positionOffset, positionOffsetPixels);
if (positionOffset == 0) // FIXME
loadHistoryPage(position);
}

@Override
public void onPageSelected(int position) {}

@Override
public void onPageScrollStateChanged(int state) {}


// -------------------------------------------------------------------------------------------
// -------------- Helper Methods
// -------------------------------------------------------------------------------------------


public void setListView(ListView listview)
{
mListView = listview;
}

public void setProgressBar(ProgressBar progressBar)
{
mProgressBar = progressBar;
}

private void loadHistoryPage(int position)
{
mListView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
mListView.setAdapter(null);
mListView.setAdapter(mAdaptersList.get(position-1));
mProgressBar.setVisibility(View.INVISIBLE);
mListView.setVisibility(View.VISIBLE);
}


}

Answer

When you use PageIndicator in conjunction with the Viewpager then the onPageChangeListener of the ViewPager is not called. I don't know the reason for this but this happened when I was using both. May be cause the page indicator consumes the page change event. Instead you should set a page change listener to the PageIndicator and it will be called when the page changes. Here is how you can do this:

indicator.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected (int page)
        {
            //page changed
        }

        @Override
        public void onPageScrolled (int arg0, float arg1, int arg2)
        {
        }

        @Override
        public void onPageScrollStateChanged (int arg0)
        {
        }
    });

I hope this will help. Feel free to discuss.