erdna erdna - 9 months ago 67
Android Question

How to implement endless list with RecyclerView?

I like to change from ListView to RecyclerView. I miss the onScroll of the OnScrollListener at RecyclerView to determine that the user scrolled to the end of the list. How do I know that the user scrolls to the end of the list so that I can fetch new data from a Rest Service.


Thanks to @Kushal and this is how I implemented it

private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
        if(dy > 0) //check for scroll down
            visibleItemCount = mLayoutManager.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

            if (loading) 
                if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
                    loading = false;
                    Log.v("...", "Last Item Wow !");
                    //Do pagination.. i.e. fetch new data

Don't forget to add

LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);