My listview lags when scrolling. It seems as the lazyload works but the lag happens everytime an image is shown.
I'm using the imageLoader found here http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ and this is my adapter:
public class EventAdapter extends ArrayAdapter<Event>{
public ListImageLoader imageLoader;
public DisplayImageOptions imgDispOpts;
private ArrayList<Event> objects;
public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> objects,ListImageLoader imageLoader) {
super(context, textViewResourceId, objects);
this.objects = objects;
this.imageLoader = imageLoader;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
@SuppressLint("DefaultLocale")
@Override
public View getView(int position, View convertView, ViewGroup parent){
Event event = objects.get(position);
// assign the view we are converting to a local variable
View v = convertView;
ViewHolder holder;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
System.out.println("new Viewholder");
holder = new ViewHolder();
holder.listTimeString = (TextView) v.findViewById(R.id.listTimeString);
holder.date =(TextView) v.findViewById(R.id.Date);
holder.title = (TextView) v.findViewById(R.id.title);
holder.shortInfo = (TextView) v.findViewById(R.id.shortinfo);
holder.ageIcon = (ImageView) v.findViewById(R.id.listAgeIcon);
holder.thumb = (ImageView) v.findViewById(R.id.wideListImage);
holder.header = (RelativeLayout) v.findViewById(R.id.headerLayout);
holder.rowLayout = (RelativeLayout) v.findViewById(R.id.rowlayout);
holder.listVenueIcon = (ImageView) v.findViewById(R.id.listVenueIcon);
holder.eventRowLayout = (RelativeLayout) v.findViewById(R.id.eventrowlayout);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
if (event != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
//SET FONTS
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/myriad.otf");
holder.title.setTypeface(tf);
holder.shortInfo.setTypeface(tf);
Typeface tf2 = Typeface.createFromAsset(getContext().getAssets(),"fonts/agencyr.ttf");
holder.date.setTypeface(tf2);
// check to see if each individual textview is null
// if not, assign some text!
if(event.isBig()){
//holder.rowLayout.getLayoutParams().height=230;
holder.shortInfo.setHeight(80);
holder.shortInfo.setMaxLines(5);
holder.shortInfo.setText(event.getInfo());
}
else{
//holder.rowLayout.getLayoutParams().height=125;
//holder.listVenueIcon.setImageResource(R.drawable.ikon_bar);
holder.shortInfo.setHeight(16);
holder.shortInfo.setMaxLines(1);
holder.shortInfo.setText(event.getInfo());
}
if(event.isClub()){
holder.listVenueIcon.setImageResource(R.drawable.ikon_klubb);
}
else{
holder.listVenueIcon.setImageResource(R.drawable.ikon_bar);
}
if (event.isNewDay()){
holder.date.setText(event.getDay().toUpperCase());
holder.header.setVisibility(View.VISIBLE);
} else{
holder.header.setVisibility(View.GONE);
}
if (holder.title != null){
holder.title.setText(event.getHost().toUpperCase());
}
if(holder.ageIcon!= null){
switch(Integer.parseInt(event.getAge())){
case(19):
holder.ageIcon.setImageResource(R.drawable.event_info_19);
break;
...
more cases
...
case(30):
holder.ageIcon.setImageResource(R.drawable.event_info_30);
break;
default:
holder.ageIcon.setImageResource(R.drawable.event_info_18);
break;
}
}
if(holder.thumb != null){
imageLoader.DisplayImage(event.getThumbURL(), holder.thumb);
}
if(holder.listTimeString != null){
String tempTimeString = event.getStartTime() + " - " + event.getEndTime();
holder.listTimeString.setText(tempTimeString);
}
}
// the view must be returned to our activity
return v;
}
static class ViewHolder{
RelativeLayout eventRowLayout;
TextView listTimeString;
TextView date;
TextView title;
TextView shortInfo;
ImageView ageIcon;
ImageView thumb;
RelativeLayout header;
RelativeLayout rowLayout;
ImageView listVenueIcon;
}
}
Any ideas?
EDIT: I am loading the imageURLs asynchronously at onCreate, this would be a perfect place to cache the images, can you see how this could be done using the ImageLoader in the link?