Android - how can i know when gridview has reached the bottom?
Asked Answered
T

2

13

I'm building an app that loads images from JSON and displays them as grid view. 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. so, how can i tell when the grid enter image description hereview 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.

Tavie answered 28/11, 2013 at 23:53 Comment(0)
M
29

You can use setOnScrollListener to your 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){

    }
});
Metzler answered 29/11, 2013 at 0:0 Comment(4)
what if there are less items then a pages length?? it would be fired as soon it startsXymenes
It was calling twice.Somme
@Xymenes is right. It will always fired as soon as it starts and continouslyEddington
There is some good examples #26096357Sconce
P
1

And here is my solution, You may want to simply override the onOverScrolled method :

@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
       }    
}
Preamplifier answered 24/3, 2019 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.