How to implement setOnScrollListener in RecyclerView

后端 未结 7 972
遇见更好的自我
遇见更好的自我 2020-11-29 02:09

How Do I show progress bar at bottom when user reached to items those are visible in a list.

I have written a code in which i am getting data using web service, now

7条回答
  •  春和景丽
    2020-11-29 02:43

    Another example. Set you progress bar at bottom and change its visibility according to scrolling/loading and your records. Note: you need to call notifyDataSetChanged(); method to add/refresh data to adapter

      recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int total = linearLayoutManager.getItemCount();
                    int firstVisibleItemCount = linearLayoutManager.findFirstVisibleItemPosition();
                    int lastVisibleItemCount = linearLayoutManager.findLastVisibleItemPosition();
    
                    //to avoid multiple calls to loadMore() method
                    //maintain a boolean value (isLoading). if loadMore() task started set to true and completes set to false
                    if (!isLoading) { 
                        if (total > 0) 
                            if ((total - 1) == lastVisibleItemCount){
                               loadMore();//your HTTP stuff goes in this method 
                               loadingProgress.setVisibility(View.VISIBLE);
                            }else
                               loadingProgress.setVisibility(View.GONE);
                    }
                }
    
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
    
                }
    
        });
    

提交回复
热议问题