Question

I'm using a GridView to display some images on my application.

I'm also using UniversalImageLoader to display these images as I got their Uri (with a i).

I've set up a View holder in my adapter but when I scroll the GridView it's laggy as hell. This is inacceptable.

Here is my code:

public class GridViewAdapter extends ArrayAdapter<Object>
{
    private Context context = null;
    private List<Object> objects = null;
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public CountDownAdapter(Context context, List<Object> objects)
    {
        super(context, android.R.layout.simple_list_item_1, objects);
        this.context = context;
        this.objects = objects;

        this.imageLoader = ImageLoader.getInstance();
        if (!this.imageLoader.isInited()) this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_downloading)
            .showImageForEmptyUri(R.drawable.ic_downloading)
            .showImageOnFail(R.drawable.ic_downloading)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        CountDownHolder holder = null;

        if (convertView == null)
        {
            LayoutInflater layoutInflator = LayoutInflater.from(getContext());
            convertView = layoutInflator.inflate(R.layout.adapter_gridview, null);

            holder = new CountDownHolder();
            holder.img_thumbnail = (ImageView) convertView.findViewById(R.id.img_thumbnail);
            holder.text_event = (TextView) convertView.findViewById(R.id.text_event);
            holder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            holder.text_daysleft = (TextView) convertView.findViewById(R.id.text_daysleft);
            convertView.setTag(holder);
        }
        else
        {
            holder = (CountDownHolder) convertView.getTag();
        }

        final Object object = this.objects.get(position);

        // Apply background
        imageLoader.displayImage("file://" + countDown.getThumbnail().getPath(), holder.img_thumbnail, options);
        //holder.img_thumbnail.setImageURI(countDown.getThumbnail());

        // Format date
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy", Locale.getDefault());
        String date = formatter.format(new Date(countDown.getDateEvent()));

        // Format event
        String event = countDown.getEvent().substring(0, 1).toUpperCase(Locale.getDefault()) + countDown.getEvent().substring(1).toLowerCase(Locale.getDefault());

        // Format days
        String days = null;
        if (countDown.wasBefore() && countDown.getTimeRemaining() == 1) days = context.getString(R.string.main_yesterday);
        else if (countDown.getTimeRemaining() == 0) days = context.getString(R.string.main_today);
        else if (countDown.getTimeRemaining() == 1) days = context.getString(R.string.main_tomorrow);
        else days = countDown.getTimeRemaining() +" "+ context.getString(R.string.main_days);

        // Set values
        holder.text_event.setText(event);
        holder.text_date.setText(date);
        holder.text_daysleft.setText(days);

        // Attach listener
        // ...

        return convertView;
    }

    public List<Object> getObjects()
    {
        return objects;
    }

    public void setObjects(List<Object> objects)
    {
        this.objects = objects;
    }
}

Did I make something wrong or did I forget to do something important ?

Thanks for your help.

Was it helpful?

Solution

Problem solved:

I was using a custom font which was initialized in the getView method. Just moving it to the constructor solved the lag issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top