android - Why onViewScrollFinished is called multiple times in HorizontalPager.OnScrollListener() interface

205 views Asked by At

To manage horizontal pager, I am currently using the source code from here I have noticed when i slide from one page to another, method onViewScrollFinished() is called upto 5 times instead of one. Though i have managed this multiple calling, but i need to know is there any ticks to handle this.

Here is my source code for managing onViewScrollFinished()

public void onViewScrollFinished(int currentPage) {  

            count++;
            if( (previousPage != currentPage)){
                Toast.makeText(MainActivity.this, ""+(currentPage+count), Toast.LENGTH_SHORT).show(); 
            }

            previousPage = currentPage;

}

Where PreviousPage is set to -1;

1

There are 1 answers

1
Xaver Kapeller On

I don't know why it is called multiple times, but what your are doing is completely fine. In such cases I often find myself doing something like this:

private int currentPage = Integer.MIN_VALUE;

@Override
public void onViewScrollFinished(int currentPage) {

    if( (this.currentPage != currentPage)){
        this.currentPage = currentPage;
        onPageChanged(currentPage);
    }

}

protected void onPageChanged(int currentPage) {
    // Executed only when the page really changes    
}