Gridview shuttering when scrolling I have more than 1000 items in my GridView. When scrolling though, it appears to make the app 'shutter'. Each time I load images from Picasso GC, it increases.

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class BrandsItemAdapter extends BaseAdapter {

    public Activity activity;
    private static LayoutInflater inflater = null;

    public ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;

    public HashMap&lt;String, String&gt; ITEM;

    public BrandsItemAdapter(Activity activity,ArrayList&lt;HashMap&lt;String, String&gt;&gt; data)
    {
        this.activity=activity;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        ITEM = new HashMap&lt;String, String&gt;();
        ITEM = data.get(position);

        if(convertView == null)
        {
        convertView  = inflater.inflate(R.layout.department_viewadapter, parent, false);
        holder = new ViewHolder();

        holder.mTitleView = (TextView) convertView.findViewById(R.id.textView1);
        holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);

        convertView.setTag(holder);
        }else
        {
        holder = (ViewHolder) convertView.getTag();
        }

        try
        {
            holder.mTitleView.setText(ITEM.get("name"));
            Picasso.with(activity).load(ITEM.get("image")).placeholder(R.drawable.icon).resize(150, 150).error(R.drawable.icon).into(holder.imageView1);
            System.gc();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView mTitleView;
        ImageView imageView1;
    }

}
Easy things


auto-generated comments


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}



The method is not auto-generated any more, remove the comment. (this happens in a few places).
brace-placement

In Java, the opening brace { goes at the end of the line, not the start of a new line. Code like this:


    }
    catch(Exception e)
    {
        e.printStackTrace();
    }



should look like:

} catch(Exception e) {
    e.printStackTrace();
}



Hard Stuff

This code is likely your problem:


holder.mTitleView.setText(ITEM.get("name"));
Picasso.with(activity).load(ITEM.get("image"))
       .placeholder(R.drawable.icon).resize(150, 150)
       .error(R.drawable.icon).into(holder.imageView1);
System.gc();



There are two things here.

Firstly, System.gc() in any code that is not directly related to the system itself, is almost always a bug. There should never be a need for this. Let the system look after itself. It really does not need your help.

The second item is that it may be taking picasso a long time to load the Images. If you are scrolling to unseen areas of the system then the code may need to be calling the getView() method a lot. You need this method to be fast.

Convert the Picasso line to be an AsyncTask, and let it populate the imageView in the background. That will make populating the views a lot faster