2

I'm using Picasso to display many small ImageViews in a RecyclerView. The ImageViews change their Image for 500 milliseconds when I click on them and change it back when the handler with 500 milliseconds end.

This is the code:

    Picasso.get().load(imageResources[position]).into(holder1.itemImageView);

    holder1.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            final Context context = v.getContext();
            Picasso.get().load(pressedResource[position]).into(holder1.itemImageView);
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    Picasso.get().load(imageResources[position]).into(holder1.itemImageView);
                }
            }, 500);

            new Thread(new Runnable() {
                @Override
                public void run() {


                    EventHandlerClass.startMediaPlayer(v, soundID);
                }
            }).start();

        }
    });

I use Picasso to prevent my app from getting OutOfMemory crashes. The Image Dissapears for like 10 milliseconds, then changes to the other image, after the 500 milliseconds it dissapears again and then it changes back to the default image. It only dissapears for the first time I click on one of the Images, after that it works without dissapearance. I think this happens because Picasso is loading the image too slow. Is there a way to first load the image and display it when I click on the button?

Here is a GIF which shows how it looks like: https://media.giphy.com/media/STlGbpXvT8B9iBvnQS/giphy.gif In this case it only dissapeared once but sometime it dissapeares twice with one click.

The Images are all 200x200 and around 8kb. The format is .WEBP

How can I prevent this?

HavanaSun
  • 446
  • 3
  • 12
  • 39
  • You can add a listener to Picasso and show the image after it's loaded. https://stackoverflow.com/questions/26548660/how-to-listen-for-picasso-android-load-complete-events – Lukas Oct 25 '19 at 18:26
  • @lukas thanky, can you show me how to do it in my case? WIth the code in my question above – HavanaSun Oct 25 '19 at 18:30

1 Answers1

0

The idea is to wait for the Callback and then set the image into the image view. This is achieved by using a CallbackListener.

Picasso.get().load(imageResources[position]).into(holder1.itemImageView);

holder1.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        final Context context = v.getContext();
        Picasso.get().load(pressedResource[position]).into(holder1.itemImageViewnew com.squareup.picasso.Callback() {

                    @Override
                    public void onSuccess() {
                     new Handler().postDelayed(new Runnable() {
                        public void run() {
                           Picasso.get().load(imageResources[position]).into(holder1.itemImageView);
                        }
                       }, 500);

                       new Thread(new Runnable() {
                        @Override
                        public void run() {
                         EventHandlerClass.startMediaPlayer(v, soundID);
                        }
                     }).start();
                    }

                    @Override
                    public void onError() {

                    }
                }));

    }
});
Lukas
  • 812
  • 1
  • 14
  • 43
  • Thanks for the answer but sadly it doesn't work, it makes it even worse, now it dissapears for about 20 milliseconds – HavanaSun Oct 25 '19 at 18:48
  • I am sorry but I can't help you anymore. Hope someone else can :D – Lukas Oct 25 '19 at 18:52
  • Yes thanky anyway, here is the Gif which showes the problem btw: https://media.giphy.com/media/STlGbpXvT8B9iBvnQS/giphy.gif – HavanaSun Oct 25 '19 at 19:15