How to implement endless list with RecyclerView?

前端 未结 30 2709
无人及你
无人及你 2020-11-22 02:22

I would like to change ListView to RecyclerView. I want to use the onScroll of the OnScrollListener in RecyclerView to determine if a

30条回答
  •  自闭症患者
    2020-11-22 03:04

    Although the accepted answer works perfectly, the solution below uses addOnScrollListener since setOnScrollListener is deprecated, and reduces number of variables, and if conditions.

    final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    feedsRecyclerView.setLayoutManager(layoutManager);
    
    feedsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
    
            if (dy > 0) {   
                if ((layoutManager.getChildCount() + layoutManager.findFirstVisibleItemPosition()) >= layoutManager.getItemCount()) {
                    Log.d("TAG", "End of list");
                    //loadMore();
                }
            }
        }
    });
    

提交回复
热议问题