Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show an "Loading..." image while background loading of image with Picasso

I am using Picasso for background loading of images (list view and ImagePager). I can set loading image and errorimage with Picasso, but I am unable to show a "loading in progress" Image during background loading.

Has anybody an idea how to get this done? In my PagerAdapter Class I have the following method:

        @Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(context);
    int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    // Set default Image before loading real image
    imageView.setImageResource(R.drawable.img_def);

    imageView.setId(imgViewId);
    imageView.setClickable(true);
    imageView.setOnClickListener(this);
    ((ViewPager) container).addView(imageView, 0);

    // Set default Image before loading real image
    Picasso.with(context).load(R.drawable.img_def).into(imageView);

    // Load real image with Picasso and show error image if failed
    Picasso.with(context).load(GalImgUrls[position]).error(R.drawable.no_img).into(imageView);

    return imageView;
}

However, it does not show the "loading..." image. Any solutions/ideas?

Thanks!

like image 936
Radiesel Avatar asked Mar 26 '14 08:03

Radiesel


People also ask

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.

What is placeholder in Picasso?

You can use a placeholder image by using the *.placeholder(R.drawable.image_name)* in your Picasso call like: Picasso.with(context) .load(imageUrl) .placeholder(R.drawable.image_name) If you want to use a progress bar instead of a placeholder image you can create a callback inside the .into function.

Which is better glide or Picasso?

Glide is faster and the results of Picasso and Coil are similar. But what about when we are loading from the cache. As you can see in the images below we have the best times for Glide in most of the cases.


1 Answers

Looks like I found a solution myself. Use "placeholder" image:

       Picasso.with(context).load(tnu).error(R.drawable.no_img).placeholder(R.drawable.img_def).into(holder.immoPic);

Works like charm....

like image 56
Radiesel Avatar answered Nov 12 '22 20:11

Radiesel