Android Infinite List with Load More Example
Objectives Outline for This Tutorial:
- We will be building a news feed, which will simulate JSON data loading from the internet and then populate the list items.
- Till the network data is being fetched, the list will show a loading view at the bottom.
- We will be loading the images from urls and setting it in the view display. For this purpose we will use a library Glide.
- The JSON data will be seeded in the application and this seed JSON file will be stored in the assets folder.
- The seed file will be parsed into
InfiniteFeedInfoobject using another library gson.
- This structure will also be compatible if we are pulling url json data from a live server.
This Tutorial is based on PlaceHolderView android library. This library has
InfinitePlaceHolderView class which facilitates such implementations.
This class is build upon the
PlaceHolderView . It defines the load more callback and the loading view. The detail about this class can be found here
The best approach to understand and appreciate its usage would be to go through the process described below.
Let’s start building:
Setup the project in android studio with default activity.
In app’s build.gradle add the dependencies.
- Add an
assetsfolder in the src/main directory and point to it in gradle assets.srcDirs
CardViewis used to display the images in the list
Add Internet permission in the app’s
This xml defines the list item card view.
Progress shows the loading when the data is fetched from the server.
infinite_news.json file in the
assets folder created in the above step 1.
- This strategy is very useful in bundling app with seed files. Seed files contain data build in the app package and can be used to populate database or use to display default data to the user. Placing seed files in the form of json makes is extremely easy to parse into models.
Utilscontain methods required to parse seed json file and also populate the model
@SerializedNameannotation belongs to gson class and used to read json file variable and bind it to the model variable.
@Exposeis used to make the variable readable for the gson parsing
We will now create the class to bind the item views of the list and its operations.
ItemView.java for the item view of the list.
@layoutis used to bind the xml layout with this class.
@Viewis used to bind the views in a layout we want to refer to.
@Resolvecalls a method when the view is in the memory so to populate it with the data.
For detailed explanations view PlaceHolderView at GitHub repository
We will now create the class to bind and define the load more view.
@LoadMoreThis annotation is called when the list is scrolled to the last item. Any network call to fetch more data should be done in this annotated method.
- For the simulation of delayed loading of data from the internet, I have defined
ForcedWaitedLoadingclass. This class creates a new thread and then after 2 sec, adds more data int view list, in the UI thread. The view should always be updated from UI thread.
mLoadMoreView.loadingDone()method need to be called when all the data has been fetched and view has been populated with new data. It will remove the load more view.
mLoadMoreView.noMoreToLoad()method should be called when all the data has been fetched. It will remove the load more functionality.
- We obtain the instance of the
InfinitePlaceHolderViewand add views using the
InfiniteFeedInfodata from the model list.
setLoadMoreResolvermethod adds the
LoadMoreViewobject to be used as the indicator for loading.
PlaceHolderView GitHub repository is here
The source code for this example is here