毕加索与Kotlin的回调

问题描述 投票:0回答:2

我正在用Kotlin制作Android应用程序,需要使用Picasso来下载图像。我在下面看到了这个Java代码,用于设置动画到图像,但我无法将其转换为Kotlin,因为我不知道如何在“into”函数中设置Call​​back。

Picasso.with(MainActivity.this)
       .load(imageUrl)
       .into(imageView, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        //set animations here

                    }

                    @Override
                    public void onError() {
                        //do smth when there is picture loading error
                    }
                });

有人能帮我吗 ?

我的实际代码:

Picasso.with(context)
       .load(url)
       .into(imageDiapo, com.squareup.picasso.Callback)
android kotlin picasso
2个回答
1
投票
Picasso.with(MainActivity::this)
       .load(imageUrl)
       .into(imageView, object: com.squareup.picasso.Callback {
                    override fun onSuccess() {
                        //set animations here

                    }

                    override fun onError() {
                        //do smth when there is picture loading error
                    }
                })

0
投票

你好,这是毕加索提供的一些不同的方式:

Picasso.with(context).load(path).into(imageView);

2.在我们的utils包中创建一个新文件,将其命名为picasso.kt并用下面的简单代码填写:

 public val Context.picasso: Picasso
    get() = Picasso.with(this)

3.虽然这对应于receiver对象,但我们可以在任何Context上调用以下代码:

picasso.load(path).into(imageView)
  1. 我们可以进一步扩展ImageView类,如: public fun ImageView.load(path: String, request: (RequestCreator) -> RequestCreator) { request(getContext().picasso.load(path)).into(this) }
© www.soinside.com 2019 - 2024. All rights reserved.