【问题标题】:Android: Detecting When ScrollView Hits Bottom [duplicate]Android:检测 ScrollView 何时触底 [重复]
【发布时间】:2011-02-10 05:25:18
【问题描述】:

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

我已经尝试了一个小时左右来检测我的 scrollView 何时到达屏幕底部。由于屏幕尺寸的不同,以及与Android无关的原因,我不能简单地说它的Y位置达到某个值时它在底部,但是我没有找到检测它何时处于底部的方法。

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

【问题讨论】:

标签: android scroll position scrollview


【解决方案1】:

试试这个:

@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

【讨论】:

  • 根据我的经验,您应该检查 if(diff
  • 如果它恰好返回 -1 是有意义的。
  • 只是为了表扬,答案是从marteinn.se/blog/?p=485复制的
  • 一个 ScrollView 只能承载一个直接子元素,因此该行和 Cast: (View) getChildAt(getChildCount()-1);是不必要的。
  • 你也可以覆盖 onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) 并检查clampedY是否为真
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2016-07-08
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多