Android:当ScrollView触及底部时检测

时间:2011-02-10 05:25:19

标签: android scroll position scrollview

  

可能重复:
  How to trigger an event when scrollView reach the bottom with Android?

我已经尝试了一个小时左右来检测我的scrollView何时到达屏幕底部。由于各种屏幕尺寸和Android没有涉及的内容,我不能简单地说它在Y位置达到一定值时位于底部,但是没有方法可以检测到它在底部是否找到了。

我确信这有一个简单的解决方案,通过其他变量或其他东西减去视图的高度,但由于某种原因它只是没有点击我。任何想法都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:82)

试试这个:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) 
{
    // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
    View view = (View) getChildAt(getChildCount()-1);

    // Calculate the scrolldiff
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    // if diff is zero, then the bottom has been reached
    if( diff == 0 )
    {
        // notify that we have reached the bottom
        Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
    }

    super.onScrollChanged(l, t, oldl, oldt);
}

要实现此功能,请展开ScrollView,然后覆盖onScrollChanged方法(继承自View)。

参考:Android: Understanding when ScrollView has reached the bottom

相关问题