How to implement load more recyclerview in android

前端 未结 8 1870
盖世英雄少女心
盖世英雄少女心 2020-12-30 06:23

I want to implement load more in Recyclerview. Here is the code. The code is from github. https://gist.github.com/ssinss/e06f12ef66c51252563e

MainActivity code:

8条回答
  •  执念已碎
    2020-12-30 06:36

    I found an answer here that, I believe, is much better than most I've seen on SO and elsewhere.

    The idea is simple: in onScrolled in your RecyclerView's ScrollListener, check if the last completely visible item is the last item in your data set.

            if(llm.findLastCompletelyVisibleItemPosition() == data.length() -1){
                //bottom of list!
                loadMoreData();
            }
    

    This happens with a method in the LinearLayoutManager. Calling LinearLayoutManager#findLastCompletelyVisibleItemPosition() can comparing it to the position of the last item in your dataset let's you know when you can load more.

    I haven't tried this for the GridLayoutManager.

    UPDATE

    LinearLayoutManager#findLastVisibleItemPosition() is a better alternative to LinearLayoutManager#findLastCompletelyVisibleItemPosition(), especially when your items are longer than the window height.

提交回复
热议问题