You can use EndlessRecyclerView
Overview A common feature of the application is to automatically load more elements when the user scrolls through the elements (or endless scrolling). This is done by running a request for more data as soon as the user crosses the threshold of the remaining elements before they reach the end.
The approaches to ListView, GridView, and RecyclerView (the successor to ListView) are described here. Both of them are similar in code, except that the LayoutManager in the RecyclerView must be passed in to provide the necessary information for implementing endless scrolling.
In both cases, the information needed to implement scrolling involves determining the last visible item in the list and some type of threshold value to start receiving more data before reaching the last item. This data can be used to determine when to load more data from an external source: To ensure that endless scrolling appears, it is important to get the data before the user reaches the end of the list. Therefore, adding a threshold value allows you to expect the addition of additional data.
Implementation with ListView or GridView
Each AdapterView (such as ListView and GridView) supports binding to OnScrollListener events, which are fired whenever a user scrolls a collection. Using this system, we can define a base EndlessScrollListener that supports most use cases by creating our own class that extends OnScrollListener:
import android.widget.AbsListView; public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
Note that this is an abstract class and that to use it you must extend this base class and define the onLoadMore method to actually receive the new data. Now we can define an anonymous class as part of any action that extends the EndlessScrollListener and associates it with the AdapterView. For instance:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {
Now, when scrolling, the elements will be automatically filled, because the onLoadMore method will be launched after the user crosses the visible Threshold. This approach works equally well for a GridView, and the listener provides access to both the page and the totalItemsCount to support paginated and offset based selection.
Implementation with RecyclerView We can use a similar approach with RecyclerView by defining the EndlessRecyclerViewScrollListener interface for which the onLoadMore () method should be implemented. The LayoutManager, which responds to the RecyclerView for rendering where the elements should be located and controls the scrolling, provides information about the current scroll position relative to the adapter. For this reason, we need to pass an instance that the LayoutManager is used to collect the necessary information to determine when to load more data. Implementing infinite pagination for RecyclerView requires the following steps:
Implementing infinite pagination for RecyclerView requires the following steps:
1. Copy the EndlessRecyclerViewScrollListener.java file to your application. 2. Call addOnScrollListener (...) in the RecyclerView to enable infinite pagination. Pass an instance of EndlessRecyclerViewScrollListener and implement onLoadMore, which runs whenever you need to load a new page to populate the list.
3. Inside the above onLoadMore method, load additional elements into the adapter either by sending a network request or by downloading from another source.
To start processing the scroll events for steps 2 and 3, we need to use the addOnScrollListener () method in our Activity or Fragment and pass the layout manager in the EndlessRecyclerViewScrollListener instance, as shown below:
public class MainActivity extends Activity {
https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView