Best android open-source packages and libraries.

RecyclerView Pagination using MVP

Basic Pagination with RecyclerView using MVP (Model View Presenter) Android Architecture
Updated 1 year ago

RecyclerView-Pagination-using-MVP

  • What, Why and When of Pagination: Pagination is the process of dividing a document into discrete pages, either electronic pages or printed pages.

  • Why Pagination?

    I’m sure you have a pretty good idea by now on when to use it. If you have a ton of content that takes too long to load. This can be either from a local database or an API call. Then it makes sense to use Pagination. If you’re pulling from a database, request data in batches (say 15 per request). The same also holds true for an API call.

Getting Started

  • Android Pagination with RecyclerView

      products_rclv.addOnScrollListener(new RecyclerView.OnScrollListener()
      {
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy)
          {
              super.onScrolled(recyclerView, dx, dy);
    
              int visibleItemCount = linearLayoutManager.getChildCount();
              int totalItemCount = linearLayoutManager.getItemCount();
              int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
    
              if (!isLoading)
              {
                  if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0)
                  {
                      isLoading = true;
    
                      pageIndex++;
    
                      loadMoreItems();
                  }
              }
          }
      });
    
  • NOTE:

The onScrolled() logic is the most important piece of your entire Pagination logic. So make sure you’re doing it right. The key snippet which contains the Pagination logic is as follows.

    if (!isLoading)
      {
          if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0)
          {
              isLoading = true;

              pageIndex++;

              loadMoreItems();
          }
      }

Demo

RecyclerView-Pagination-using-MVP

Basic MVP Sample

  • MVP - Basic Android Architecture MVP (Model View Presenter) pattern

Prerequisites

  • Android Studio 3.0
  • Android Device with USB Debugging Enabled

Built With

  • Android Studio - The Official IDE for Android
  • Java - The Official Language for Android
  • Gradle - Build tool for Android Studio

Thanks for reading this repo. Be sure to click ★ below to recommend this repo if you found it helpful. It means a lot to me.

For more about programming, follow me on Medium

Also, Let’s become friends on Linkedin