Android Expandable News Feed Example
Tutorial using PlaceHolderView
This view is very different from the Android ExpandableListView in the sense that it’s based on PlaceHolderView which is based on RecyclerView. So, we have the power of reusing the views and managing the memory, that too is a very modular and simple interface.
Objectives Outline for this tutorial:
- We would be building a news feed with expandable parent view. On expansion of the one view group collapses the other views.
- 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 feed data 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 Feed object using another library gson.
- This structure will also be compatible if we are pulling url json data from a live server.
This view is build upon the PlaceHolderView with the parent child architecture. The detail about this class can be found here
The best approach to understand and appreciate its usage over traditional android ExpandableListView would be to go through the process described below.
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 src/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
Setup project color and drawables:
- Download the icon for the expand and collapse indicator from https://design.google.com/icons/ and place them in the drawable folder.
- Create two drawable xml files to mark a line between item views
Place 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.
- Utils contain methods required to parse seed json file and also populate the model Feed.java and Info.java
Create model Feed.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
Create model Info.java
We will now create the class to bind the item views and its operations.
Create HeadingView.java for the parent view
- @Parent is annotation used to bind a class as the parent view.
- @layout is used to bind the layout with this class
- @SingleTop is used to keep only one parent in expanded state and others in collapsed state.
- @View is used to bind the views in a layout we want to refer to
- @Toggle is used to provide a view in the layout to be used as a toggle for expanding or collapsing a parent on view click. If not provided then the parent view is used as a toggle view by default.
- @ParentPosition is used bind an int variable to be updated with the relative position of a parent with respect to other parents.
- @Expand is used to get a method invoked when the parent view expands.
- @Collapse is used to get a method invoked when the parent view collapses.
For detailed explanations view PlaceHolderView at GitHub repository
Create InfoView.java for the child views
- Most annotations used are described in step 10.
- @ChildPosition is used to bind an int variable to be updated with the relative position among children of a parent.
- We obtain the instance of the ExpandablePlaceHolderView and add views using the feed data from the model list.
- Parent and child should be added in sequence and parent must be added before its child
PlaceHolderView GitHub repository is here
The source code for this example is here