Endless RecyclerView with ProgressBar for pagination

前端 未结 9 2175
庸人自扰
庸人自扰 2020-11-27 12:51

I am using a RecyclerView and fetching objects from an API in batches of ten. For pagination, I use EndlessRecyclerOnScrollListener.

It\'s all working properly. Now

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 13:16

    This solution is inspired by Akshar Patels solution on this page. I modified it a bit.

    1. When loading the first items it looks nice to have the ProgressBar centered.

    2. I didn't like the remaining empty padding at the bottom when there existed no more items to load. That has been removed with this solution.

    First the XML:

    
    
    
    
    
    

    Then I added the following programmatically.

    When first results been loaded, add this to your onScrollListener. It moves the ProgressBar from center to the bottom:

    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) loadingVideos.getLayoutParams();
    layoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
    loadingVideos.setLayoutParams(layoutParams);
    

    When no more items exist, remove the padding at the bottom like this:

    recyclerView.setPadding(0,0,0,0);
    

    Hide and show your ProgressBar as usual.

提交回复
热议问题