Android: How can I load more data in the background in recyclerView

邮差的信 提交于 2019-12-04 22:29:17

Refer this. Its explained in a cool fashion here.

Basically you need an onScrollChangeListener to detect scroll changes in your recyclerview but that alone wont help. So you need a way to check whether you are currently reaching the bottom of your recyclerview. so here's how you do it:

Check this line in the above link

// If it isn't currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
            loading = onLoadMore(currentPage + 1, totalItemCount);
        }

See the variable visibleThreshold that is where you modify and apply you own custom value to make the second call.

lets check that with values:

if visibleThreshold is 0 and you have 10 items in your list and you`re on the 6 item and the first item is the 0th item: the this line

if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount )

interprets as -> if (true && (0+6+0)>=10) which interprets as false!

thus the second call wont be made now. it will be made when you lastitem is the 10th item (as your threshold is 0)

But now if you add a threshold of 4, the next call is made as the condition now is satisfied: interprets as -> if (true && (0+6+4)>=10) which interprets as true!

Summarizing, the visibleThreshold is where you add your logic, rest you can implement the boiler plate code from the link!

For pagination I use following code

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (hasMoreRequest) {
                    if (dy > 0) { //check for scroll down
                        checkForMoreLoad();
                    }
                }
            }
        });

private void checkForMoreLoad() {
        final Handler handler = new Handler();
        Runnable checkForMoreData = new Runnable() {
            @Override
            public void run() {
                if (null != recyclerView) {
                    int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
                    int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                    int pastVisibleItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
                    onScrollToLoad(visibleItemCount, pastVisibleItems, totalItemCount);
                }
            }
        };
        handler.postDelayed(checkForMoreData, 100);
    }

    private void onScrollToLoad(int visibleItemCount, int pastVisibleItems, int totalItemCount) {
        if ((visibleItemCount + pastVisibleItems) >= totalItemCount && hasMoreRequest) {
            if (BuildConfig.DEBUG)
                Log.d(TAG, "onScroll lastInScreen - so load more");
            startApi(page + 1);
        }
    }

This code will call API for next page, whenever scroll reaches to bottom of the scroll.

Also after each API, I call checkForMoreLoad() method again to check whether more data is required or not. And yes don't forget to add adapter.notifyItemRangeChanged() after adding new elements to adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!