Android Advance Image Gallery Example
json, image url loading, nested image views, PlaceHolderView
Before Moving on to this tutorial I would request you to go through the Android Beginner Image Gallery Example.
Objectives Outline for this tutorial:
- We would be building a list of images with two types of views. One on the top with horizontal image list and below with vertical image list and apply animations on these view items as shown in the above display image.
- We will be loading the images from urls and setting it in the view display. For this purpose we will be using a library Glide.
- The Urls list 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 Image object using another library gson.
- This structure will also be compatible if we are pulling url json data from a live server.
Let’s start building:
Set up the project in android studio with default activity.
In app’s build.gradle add the dependencies.
- Add an assets folder in the main directory and point to it in gradle assets.srcDirs
- CardView is used to display the image in the list
Add Internet permission in the app’s AndroidManifest.xml
Place images.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 used to display default data to the user. Placing seed files in the form of json makes is extremely easy to parse into models.
- Utils contain the methods required to parse seed json file and also populate the model Image.java
Create model Image.java
- @SerializedName annotation belongs to gson class and used to read json file variable and bind it to the model variable.
- @Expose is used to make the variable readable to the gson
We will now create the class to bind the item views and its operations.
- @NonReusable is annotation in PlaceHolderView, to be used in cases where we want to release all the resources and references if removed from the list and won’t use the same object in addView() method in PlaceHolderView.
- @layout is used to bind the layout with this class
- @View is used to bind the views in this layout we want to refer to
- @Resolve is used to operate on the view references obtained from @View, in short if we want to define any operation on the view it should be put in a method and annotated with @Resolve
- @Animate() is used to assign defined animations in the Animation class
- @LongClick is used to attach the long click listener and the item is removed from the list on event in this case
For detailed explanations view PlaceHolderView at GitHub repository
- This class is required to create a nested list view
- The Nesting is done by using the PlaceHolderView inside main view
- The PlaceHolderView is instantiated and views are added to it.
- We obtain the instance of the PlaceHolderView and add views using the url from the model list.
- Top 10 images are added to top list
- Rest are added to bottom view in reverse order.
PlaceHolderView GitHub repository is here
The source code for this example is here