【问题标题】:android - Why onViewScrollFinished is called multiple times in HorizontalPager.OnScrollListener() interfaceandroid - 为什么在 Horizo​​ntalPager.OnScrollListener() 接口中多次调用 onViewScrollFinished
【发布时间】:2014-03-18 07:58:53
【问题描述】:

为了管理水平寻呼机,我目前正在使用来自here的源代码 我注意到,当我从一页滑到另一页时,方法 onViewScrollFinished() 最多被调用 5 次而不是 1 次。虽然我已经管理了这个多次调用,但我需要知道是否有任何滴答声来处理这个问题。

这是我管理onViewScrollFinished()的源代码

public void onViewScrollFinished(int currentPage) {  

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

            previousPage = currentPage;

}

PreviousPage 设置为 -1;

【问题讨论】:

    标签: android pager


    【解决方案1】:

    我不知道为什么会多次调用它,但是您正在做的事情完全没问题。在这种情况下,我经常发现自己在做这样的事情:

    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    
    }
    

    【讨论】:

    • 感谢 Xaver Kapeller。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多