4
votes

I am wondering why the view in first position is repeatedly called when I scroll the gridview even when I set the adapter. This causes my gridview cannot be scrolled smoothly as it repeatedly reload image.

It seems Picasso.with(context).load(file).into(viewImage); causes the repeatedly called because if I delete this line, the gridview can be scrolled smoothly.

Any suggestions for improvement? Thanks.

Implementation for gridview array adapter

class HomeBrandCVC extends ArrayAdapter<Brand> {

private Context context;
private ArrayList<Brand> brandArrayList;
private Map<Integer, File> fileMap = new HashMap<>();

public HomeBrandCVC(Context c, ArrayList<Brand> brandArrayList) {
    super(c, R.layout.homecvc, brandArrayList);
    this.context = c;
    this.brandArrayList = brandArrayList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.homecvc, parent, false);
    TextView nameLabel = (TextView) rowView.findViewById(R.id.homecvc_nameLabel);
    nameLabel.setVisibility(View.INVISIBLE);
    final Brand brand = brandArrayList.get(position);
    nameLabel.setText(brand.name);
    ImageView viewImage = (ImageView) rowView.findViewById(R.id.homecvc_viewImage);

    Log.d("hellllo", String.valueOf(position));

    if (fileMap.containsKey(position)) {
        viewImage.setImageBitmap(BitmapFactory.decodeFile(fileMap.get(position).getAbsolutePath()));
    } else {
        viewImage.setImageResource(R.drawable.loadingimage);
        new MyLeanCloudApp.GetImageFromDiskOrUrl(brand.imageFile, new MyLeanCloudApp.ImageHandlerGetImagesResponse() {
            @Override
            public void processFinish(File file) {
                if (context != null) {
                    if (file != null) {
                        Picasso.with(context).load(file).into(viewImage);
                        fileMap.put(position, file);
                    }
                }
            }
        }).execute();
    }
    return rowView;
}
}
1

1 Answers

0
votes

Always try to use ViewHolder Pattern for lists and girds. This will improves the performance of Lists and Grids. Other think its not only depend on Picasso but its also depend on your image url, you in you xml layout file, loading data, etc.

Try to use ViewHolder to avoid scrolling issue and improves its performance

@Override
public View getView(int position, View convertView, ViewGroup parent) {

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

        convertView.setTag(viewHolder);
    }
    else {
        viewHolder=(ViewHolder)convertView.getTag();
    }
    viewHolder.image=(ImageView)convertView.findViewById(R.id.thumbnail);
    viewHolder.title=(TextView)convertView.findViewById(R.id.title);
    viewHolder.title.setText(Html.fromHtml(values.getPosts().get(position).getTitle()));

    imageView.setImageResource(Imageid[position]);
    return convertView;
}

private static class ViewHolder{
    public static ImageView image;
    public static TextView title;
}

Other think is try to load your image from cash if its previously loaded when you are using Picasso. For that check this tutorial.