http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/
My previous tutorial AndroidCustom ListView covers customizing android list view withimage and text. In this tutorial I am going to explain even more customizedlist view like Facebook, Google+ news feed where it contains multiple imagesand texts.
I used android volley networklibrary to make http calls and download the images. As volley comes withpowerful image caching mechanism, we don’t have to much worry about caching therequests and images.
Final Output
Following is the screenshot of final output of this tutorial.
Example feed Json
To demonstrate this tutorial I created an example json feedwhich contains an array of json feed object. Each object defines single feedrow where it contains information like feed id, profilepic, profile name, timestamp, statusmessage and feedimage.
|
In real world scenario, this feed json should be generateddynamically by reading a database.
1. Planning the Layout
Before start writing the code, I would like to plan the layoutfirst. If you observe the feed view, it has information like name,timestamp,profile pic, feed image and url. For this we need TextView, ImageView and alistview to display the feed. Using theLinearLayout’s orientation property I have alligned the elements vertically andhorizontally.
2. Downloading Volley.jar
If you are new to android volley library, I suggest you gothrough my previous article Android Volley Tutorial tounderstand what volley library is actually for. (In simple words volley is anetworking library used to make HTTP calls)
Download the volley.jar andkeep it aside.
Now let’s start by creating a new project.
3. Creating new project
1.Create a new project in Eclipse from File ⇒New ⇒ Android Application Projectandfill all the required information.
2. Open res⇒ values ⇒dimens.xml andadd following dimensions. If you don’t have dimens.xml, create a new file andadd these values.
dimens.xml |
|
3. Alsocreate another xml file named colors.xml under res⇒ values andadd following colors.
colors.xml |
|
4. Under res⇒ drawable,create a new file called bg_parent_rounded_corner.xml and paste the below code. This xmlwill give a rounded corner background to feed item.
bg_parent_rounded_corner.xml |
|
5. Tokeep the project well organized, I am creating required packages first. Create4 packages named app, adapter, data and volley. To create newpackage, right click on src ⇒New ⇒ Package andgive package name. Example: info.androidhive.listview.app.
Following is the project structure I am trying to achieve.
6. Nowpaste the volley.jar in project libs folder.
7.Create a class named LruBitmapCache.java under volley package and add the following code.This class takes care of caching network images on disk.
LruBitmapCache.java |
|
8. Under app package create class named AppController.java and paste the following content. Thisis a singleton class which initializes global instances of required classes.All the objects related to volley are initialized here.
AppController.java |
|
9. Nowopen your AndroidManifest.xml file and add Application.java class in<application> tag. Also we need to add INTERNET permissionas this app makes network calls.
| |
AndroidManifest.xml |
|
|
|
10. Themain challenge in this project is adjusting the aspect ratio of feed image onceit is downloaded. Unfortunately volley doesn’t provide any callback method oncetheNetworkImageView is loaded. So I created a custom ImageView class with callback methods. Thisclass automatically adjusts the image height to prevent image aspect ratiodistortion.
Under your project main package create a class named FeedImageView.java and paste the below code. I took thecode from this stackoverflowanswer andmade few tweaks.
stackoverflowanswer: I'm trying out the Google's new Volley libraryand it's looking sharp and loads images quickly when I use this method setImageUrl:
holder.image.setImageUrl(url,ImageCacheManager.getInstance().getImageLoader());
I want to add toit a call back/listener method that will fire up when loading is finished, so Ican remove the progressBar view and show the image. It's an option that existsin Universal Image Loader and Picasso libraries, but for some reason I can't find a wayto do that in Volley, tried to Google different options but so far haven't found anyreference.
Does some has acode sample to illustrate how it's done?
You can use this FeedImageView in your xml layout like this
|
|
FeedImageView.java | |
|
11. Openyour layout for main activity (activity_main.xml)and add a list view element for the feed list.
activity_main.xml |
|
12.Create another layout file named feed_item.xml under res⇒ layout folder.This layout file represents each individual feed item row in the list view.
feed_item.xml |
|
13. Under data package, create a class named FeedItem.java.This is a POJO class used to create objects for each feed item while parsingthe json. The feed item object contains information like profile pic, name,timestamp, status message, url and feed image.
FeedItem.java |
|
14. Nowunder adapter package create a class named FeedListAdapter.java.This is a custom adapter class for feed list view. This adapter class takescare following things.
> Displayingfeed data like name, timestamp, profile pic, status message and feed image.
> Converts timestamp into x minutes/hours/days ago format
> Makes URLclickable byusingurl.setMovementMethod(LinkMovementMethod.getInstance())
FeedListAdapter.java |
|
15. Nowwe have all the required classes in place. Open your main activity classMainActivity.java and add the following code. Here usingvolley JsonObjectRequest I fetched the json, parsed it(createdarray of feed item objects) and passed the data to list view adapter.
MainActivity.java |
|
Now run the project and test it once. Make sure that youremulator/device is having internet connect before you test.