安卓:当检测滚动型触底 [英] Android: Detecting When ScrollView Hits Bottom

查看:164
本文介绍了安卓:当检测滚动型触底的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/1340528/how-to-trigger-an-event-when-scrollview-reach-the-bottom-with-android">How触发事件时滚动视图到达底部与Android?

我一直在尝试了一个小时左右,以检测当我的滚动视图到达屏幕的底部。由于各种屏幕尺寸,什么没有涉及与Android,我不能简单地说这是在底部时的Y位置达到一定值,但是没有方法检测时,它是我发现的底部。

I have been trying for an hour or so to detect when my scrollView gets to the bottom of the screen. Due to the various screen sizes and what not involved with Android, I can't simply say it's at the bottom when its Y position reaches a certain value, but there's no method that detects when it is at the bottom that I found.

我敢肯定有一个简单的解决办法,通过一些其他的变量或东西减去视图的高度,但由于某种原因,它只是不点击我。任何想法将AP preciated,谢谢你。

I'm sure there's a simple solution to this, subtracting the height of the view by some other variable or something, but for some reason it just isn't clicking for me. Any ideas would be appreciated, thank you.

推荐答案

试试这个:

@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);
}

要实现这一点,延长滚动型然后重写 onScrollChanged 方法(从继承查看)。

To implement this, extend ScrollView and then override the onScrollChanged method (inherited from View).

参考:安卓了解何时滚动型已达到底部

这篇关于安卓:当检测滚动型触底的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆