1

I have an listView with an image in each of its items.

I want to have a progressBar whenever the image is not yet loaded in each item.

Including when the list is recycled and the placeholder is shown again.

In addition I when I used to download the images myself without Picasso

I used to set the imageUrl as the tag for each item and check the url is still there before I load the image (or it's replaced because of recycle and then there is no need to load the callbacked image).

How do Picasso handle this recycle?

How can I configure Picasso image cache?

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

1 Answers1

0

You have to add

progressBar.setVisibility(View.VISIBLE);

in your getView() like this :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    ImageView imageView = (ImageView) vi.findViewById(R.id.imageView);
    final ProgressBar progressBar = (ProgressBar) vi.findViewById(R.id.progressBar);
    TextView textView = (TextView) vi.findViewById(R.id.textView);

    textView.setText("" + position);

    progressBar.setVisibility(View.VISIBLE);

    Picasso.with(activity.getApplicationContext()).load(imagePath).resize(100, 100).centerCrop()
            .into(imageView, new Callback() {
                @Override
                public void onSuccess() {
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onError() {
                    //error
                }
            });

    return vi;
}

To know more details about view recycling, follow this link.

Community
  • 1
  • 1
Paritosh
  • 2,097
  • 3
  • 30
  • 42
  • thanks, it works. but I don't get it. if the item is recycled and not re-downloaded by picasso - wouldn't the progressBar stays forever? – Elad Benda2 Aug 14 '14 at 06:39
  • getView() gets called whenever your view is generated (for the first time data loading) or view is getting recycled (data changing). Now whenever getView() is called, we are calling progressBar.setVisibility(View.VISIBLE); so progressBar is displayed for the item getting loaded. Once image is downloaded, its visibility is gone. Again if row is getting loaded, the same progressBar is getting displayed. And it goes on... Hope this explains the answer. – Paritosh Aug 14 '14 at 06:59