Picasso Callback with Kotlin
Picasso.with(MainActivity::this)
.load(imageUrl)
.into(imageView, object: com.squareup.picasso.Callback {
override fun onSuccess() {
//set animations here
}
override fun onError(e: java.lang.Exception?) {
//do smth when there is picture loading error
}
})
In Latest version onError
recives an Exception
as parameter and uses get()
instead of with()
:
Picasso.get()
.load(imageUrl)
.into(imageView, object :Callback{
override fun onSuccess() {
Log.d(TAG, "success")
}
override fun onError(e: Exception?) {
Log.d(TAG, "error")
}
})
and On the previous version
Picasso.with(MainActivity::this)
.load(imageUrl)
.into(imageView, object: Callback {
override fun onSuccess() {
Log.d(TAG, "success")
}
override fun onError() {
Log.d(TAG, "error")
}
})
Hi here are some different ways that Picasso provides:
Picasso.with(context).load(path).into(imageView);
2.create a new file inside our utils package, call it picasso.kt and fill it with simple code below:
public val Context.picasso: Picasso
get() = Picasso.with(this)
3. While this corresponds to the receiver object we can invoke following code on any Context:
picasso.load(path).into(imageView)
We can go further and extend ImageView class like:
public fun ImageView.load(path: String, request: (RequestCreator) -> RequestCreator) { request(getContext().picasso.load(path)).into(this) }