Skip to content Skip to sidebar Skip to footer

Onscrolllistener Updating Gridview

I have a grid view and want to update the items in grid view after the end of list is reached. To implement this i decided to use OnScrollListener. But the behavior of OnScrollList

Solution 1:

You are seeking an endless view. You have to create an endless scroll listener and apply it to GridView. Try below code;

Initialize GridView and apply adapter

finalGridViewg= (GridView) findViewById(R.id.myGrid);
        g.setAdapter(newImageAdapter(this));
        EndlessScrollListener scrollListener=newEndlessScrollListener(g,newRefreshList() {

            @OverridepublicvoidonRefresh(int pageNumber) {
                System.out.println("On Refresh invoked..");

            }
        });
        g.setOnScrollListener(scrollListener);

EndlessScrollListener

publicclassEndlessScrollListenerimplementsOnScrollListener {

    private GridView gridView;
    privateboolean isLoading;
    privateboolean hasMorePages;
    privateint pageNumber=0;
    private RefreshList refreshList;
    privateboolean isRefreshing;

    publicEndlessScrollListener(GridView gridView,RefreshList refreshList) {
        this.gridView = gridView;
        this.isLoading = false;
        this.hasMorePages = true;
        this.refreshList=refreshList;
    }

    @OverridepublicvoidonScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (gridView.getLastVisiblePosition() + 1 == totalItemCount && !isLoading) {
            isLoading = true;
            if (hasMorePages&&!isRefreshing) {
                isRefreshing=true;
                refreshList.onRefresh(pageNumber);
            }
        } else {
            isLoading = false;
        }

    }

    @OverridepublicvoidonScrollStateChanged(AbsListView view, int scrollState) {

    }

    publicvoidnoMorePages() {
        this.hasMorePages = false;
    }

    publicvoidnotifyMorePages(){
        isRefreshing=false;
        pageNumber=pageNumber+1;
    }

    publicinterfaceRefreshList {
        publicvoidonRefresh(int pageNumber);
    }
}

Source

Solution 2:

totalDataCount -> Variable which stores the total count of data your api gets you

previousTotal -> Variable which stores the previous total count. by default it is set to zero.

loading -> Boolean value set to false by default.

Declare all these variable in your activity and not in inner class(below)

This code will work below :-

publicclassEndlessScrollListenerUserimplementsOnScrollListener {
    booleanloading=true;

    publicEndlessScrollListenerUser() {}

    @OverridepublicvoidonScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        intlastInScreen= firstVisibleItem + visibleItemCount;
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && lastInScreen==totalItemCount && totalDataCount>takeUser && (previousTotal)<totalDataCount) {

            // Your API Call Goes Here after reaching end
            loading = true;
        }
    }

    @OverridepublicvoidonScrollStateChanged(AbsListView view, int scrollState) {}
}

Solution 3:

I was able to solve the problem by adding some variables

privateintpreviousTotal=0;
        privatebooleanloading=true;
        privateintvisibleThreshold=5;

and checking the following condition ..

publicvoid onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stubif (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                    pageCount++;
                }
            }
            if (!loading
                    && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                // End has been reached
                Log.i("CountShit", String.valueOf(firstVisibleItem) + " + "
                        + String.valueOf(visibleItemCount) + " >= "
                        + String.valueOf(totalItemCount));
                // call update method here
                loading = true;
            }
        }
    });

Post a Comment for "Onscrolllistener Updating Gridview"