0

This is the code that I'm trying to convert into java but I don't understand it,actually I get this code as an answer but he/she gives me in kotlin

Glide.with(context)
            .load(url)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                    //TODO: something on exception
                }
                override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    Log.d(TAG, "OnResourceReady")
                    dummyShimmerView.visibility = View.GONE
                    postImageView.visibility = View.VISIBLE
                    return false
                }
            })
            .into(imgView)

2 Answers2

1

You can use:

     Glide
        .with(context)
        .load("url")
        .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                dummyShimmerView.setVisibility(View.GONE);
                postImageView.setVisibility(View.VISIBLE);
                return false;
            }
        })
        .into(imageView);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

I think what you are getting confused at is the RequestListener part. Kotlin uses the object notation for implementation of interfaces. So your code will translate to this roughly

 Glide.with(context).load(...)
                .listener(new RequestListener<Drawable>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
          //TODO
          return false;
        }
        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
          // Todo
          return false;
        }
      })

                }).into(imgView);
che10
  • 2,176
  • 2
  • 4
  • 11
  • I have updated the answer. Even @GabrieleMariotti has done with a more properly aligned code. You can use either. It will work. – che10 Jun 04 '21 at 07:10
  • is there a way that i can get the same functionality with java,actually I provide you the answer link then I think it will be more clear what I want to do exactly https://stackoverflow.com/a/67816307/15537898 –  Jun 04 '21 at 07:10
  • Yeah, this will work for Java, you just need to make the views visibility next like ` dummyShimmerView.setVisibility(View.GONE);` – che10 Jun 04 '21 at 07:12
  • hey brother a small request, I have given the link to the answer can you explain it a little bit, the person who answered my question just posted the code but I don't know where to exactly put that code especially the XML one –  Jun 04 '21 at 07:26
  • Inside your XML layout he is asking you to have a separate view altogether for your Shimmer layout which is of fixed height and width and then what he is implying is as soon as your image gets loaded via glide, you hide the dummy shimmer view and show your normal image View @SagarRawal This is how most shimmer libraries work, they have a dummy shimmer which shows till the read data gets loaded and then the shimmer hides. – che10 Jun 04 '21 at 07:33