简体   繁体   English

Android - 我怎么知道gridview何时到达底部?

[英]Android - how can i know when gridview has reached the bottom?

I'm building an app that loads images from JSON and displays them as grid view. 我正在构建一个从JSON加载图像并将其显示为网格视图的应用程序。 the problem is that there are hundreds of images and i want to load 20 each time, I want to download more pictures from the server when the scroll reaches the bottom. 问题是有数百个图像,我想每次加载20个,我想在滚动到达底部时从服务器下载更多图片。 so, how can i tell when the grid 所以,我怎么能告诉网格 在此输入图像描述 view reaches the bottom? 视图到底? I've searched the web and couldn't figure this one... 我在网上搜索过,无法想象这个......

anyone have ideas? 谁有想法?

I'm adding a photo of an app that does something very similar to what i want. 我正在添加一个应用程序的照片,它的功能与我想要的非常相似。 thanks. 谢谢。

You can use setOnScrollListener to your GridView. 您可以将setOnScrollListener用于GridView。

Here is the code example: 这是代码示例:

gridView.setOnScrollListener(new OnScrollListener(){
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
       {
        if(firstVisibleItem + visibleItemCount >= totalItemCount){
            // End has been reached
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState){

    }
});

And here is my solution, You may want to simply override the onOverScrolled method : 这是我的解决方案,您可能只想覆盖onOverScrolled方法:

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);

    View view = (View) getChildAt(getChildCount()-1);
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    if(diff==0) {
           //overscroll on bottom
       } else {
           //overscroll on top
       }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM