The concept here is paging of a recyclerview. We will examine various types of pagination like:
- Next/Previous Pagination
- Load more pagination
- Infinite scroll pagination.
Feel free to add more examples.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Oclemy
Android RecyclerView – Endless Pagination [Infinite Scroll]
An infinite/endless pagination example with a RecyclerView.
If we reach the end of a given page,the next page gets automatically loaded.While its being loaded we display a progressbar.We are autogenerating data using handles to simulate download of fresh data.
Overview
Classes
STEP 1 : Our Build.Gradle
STEP 2. Layout – ContentMainxml
SETP 3 : Our Spacecraft class
STEP 4 : Our RecyclerView Adapter and ViewHolder classes
STEP 5 : Our Paginator class
STEP 6 : Our MainActivity class
How to Download and Run.
More Resources
Best Regards, Oclemy.
Oclemy
Android RecyclerView Pagination – Next/Previous Pagination
Android RecyclerView Pagination – Next/Previous Pagination
In this class we see how to page/paginate data which is very important especially with RecyclerView which are meant to show large datasets.
The type of pagination we do here is the next/previous pagination. The user can navigate pages via the next/previous buttons.
If we reach the last page the next button gets disabled automatically while if we are at the first page the previous button gets disabled. In between both buttons are enabled.
Why Pagination?
Pagination is vital for not only ease of use by the user but also for smooth perfomance of the device. Users need permission because we human beings are not very good at processing huge data sets. It overwhelms when we have to scroll through a large list of data and identify an item out of it. Instead our brain is good ta getting the gist of things. Just a snapshot so that we have the big picture.
It’s why I always encourage through my tutorials adding of search as well as pagination to lists of data. Pagination allows user to break data into chunks. Users can then view a small amount at a time. Maybe 5 or 10 items.
Pagination also improves the loading time of data. Especially when data is coming from a data source like a database or cloud solutions like firebase. This is important because the android OS is very strict with resource usages in applications. So when your app has a large data set that it’ loading and this takes time, the android OS may shut down you activity due to too much memory usage. This lead to bad reputation and negative reviews in the play store and online review sites.
Paging data enables user move along the workflow quickly and efficiently.
Next/Previous Pagination
There are several pagination types like load more, 1,2,3… and endless pagination. However next/previous is the oldest of these and applies to most systems and frameworks. You basically have two buttons, one is next and the other is previous. Next allows you navigate to the next page while previous allows you navigate to previous page.
Let’s go.
1. Create Basic Activity Project
Here’s our project struecture.
2. Build.gradle
Let’s come to our app level(app folder) build.gradle:
You can see from our
build.gradle
that we aren’t using any third party libraries. Instead we have appcompat, design and cardview support libraries. We are interested in the design support as it has the recyclerview which is the list component we use. The appcompat on the other had will give us the AppCompatActivity which is ourMainActivity's
super class. Our recyclerview will be showing cardviews.3. Create User Interface
User interfaces are typically created in android using XML layouts as opposed to direct java coding.
Here are our layouts for this project:
(a). activity_main.xml
(b). content_main.xml
This layout gets included in your activity_main.xml.
We define our UI widgets here.
In this case we add a RecyclerView and two buttons(next/previous). At the root we have defined a RelativeLayout.
We have two LinearLayouts. The second one will render our next and previous buttons next to each other horizontally. That’s why it’s orientation is
horizontal
. The first one will place our recyclerview above the two buttons, that’s why we’ve set the orientation to vertical.(c). model.xml
This is the layout that defines the model of a single CardView in our RecyclerView. So we start by defining the CardView as the root view. We’ve given it several properties like the Card corner radius and card elevation. We have a TextView inside it. That textview will be used to render our data.
Lets now come to classes:
4. MyViewHolder.java
This is our RecyclerView.ViewHolder class:
This class in our RecyclerView.ViewHolder class.
It derives from
android.support.v7.widget.RecyclerView.View
thus forcing us to create a constructorpublic MyHolder(View itemView)
that takes a View object as a parameter and pass it over the base clas by a call tosuper(itemView)
.Here are the roles of this class
Here’s the ViewHolder code.
4. MyAdapter.java
This is our RecyclerView.Adapter class.
This is our RecyclerView.Adapter class.
It derives from
android.support.v7.widget.RecyclerView.Adapter<RecyclerView.ViewHolder myHolder>
class.Deriving from Recylcerview.Adapter will force us to either make our adapter class abstract or go ahead and override a couple of methods. We choose the latter, overriding
onCreateViewHolder(ViewGroup parent, int viewType)
andonBindViewHolder(MyHolder holder, final int position)
.This class will adapt our data set to our RecyclerView which we use an adapterview adapterview.
Here are the main responsibilities of this class:
onCreateVewHolder()
method.onBindViewHolder()
.6. Paginator.java
This is the class that will page/paginate our RecyclerView data.
7. MainActivity.java
So this is our main activity. It derives from
AppCompatActivity
.Here are it’s major responsibilities:
android.app.activity
.onCreate()
method.onCreate()
method of the parent Activity class and tell it of a Bundle we’ve received.activity_main.xml
into a View object and set it as the content view of this activity.More Resources