【发布时间】:2021-05-11 01:11:00
【问题描述】:
Utils 类中的HandleAPIError。为 API 错误创建 Snackbar 的自定义方法。
未解决的参考。由于接收器类型不匹配,以下候选均不适用: public fun View.snackbar(message: String, action: (() → Unit)? = ...): Unit 在文件 Utils.kt 中定义
requireview() 中 Fragment 中的接收者类型。用于活动的接收器类型。
Utils.kt
fun View.snackbar(message: String, action: (() -> Unit)? = null){
val snackbar = Snackbar.make(this, message, Snackbar.LENGTH_LONG)
action?.let {
snackbar.setAction("Retry"){
it()
}
snackbar.show()
}
}
fun Fragment.handleApiError(
failure : Resource.Failure,
retry : (() -> Unit)? = null
){
when{
failure.isNetworkError -> requireView().snackbar("Please check internet Connection", retry )
}
}
fun Activity.handleApiError(
failure : Resource.Failure,
retry : (() -> Unit)? = null
){
when{
//which receiver type used for snackbar?
failure.isNetworkError -> snackbar(
"Please check internet Connection",
retry
)
}
}
【问题讨论】:
-
你解决了吗?我有一个示例问题,你能帮我吗?
标签: kotlin android-activity view snackbar