我已經過去了一個lambda,它有一個未知類型的變量,並且在when..is條件中,該變量不能智能地轉換爲is條件中的類型。 ..它給出了這是不可能的,因爲變量是一個公共Api,有沒有解決這個問題的方法?lambda kotlin變通方法中的智能鑄造變量
1
A
回答
1
0
做的另一種方式是一個簡單的解決方法使投自己:
.onBind {
when(item) {
is Product -> view.number_sold_text = (item as Product).price.toString()
}
}
+0
當你有多個孩子的對象時,它會變得刺激,所以我認爲 – Seaskyways
2
您可以創建一個更方便的擴展onBind
˚F油膏具有item
,view
等傳遞給拉姆達而不是接收的ItemViewTypePosition
:
inline fun LastAdapter.Builder.onBind(crossinline f: (item: Any, view: View, type: Int, position: Int) -> Unit): LastAdapter.Builder {
return onBindListener(object : OnBindListener {
override fun onBind(item: Any, view: View, type: Int, position: Int) {
f(item, view, type, position)
}
})
}
用法:
builder.onBind { item, view, type, position ->
when (item) {
is Product -> view.number_sold.text = item.price.toString()
}
}
相關問題
- 1. 鑄造變量
- 2. Java變量鑄造
- 3. 鑄造會話變量 - ViewBag MVC4 - 鑄造
- 4. 變量鑄造性能比較
- 5. 確實鑄造變量的值改變?
- 6. 鑄造變量時出錯
- 7. Kotlin物化類型參數不智能鑄造
- 8. 類型鑄造變量中的類型
- 9. 在Java中鑄造參考變量
- 10. Kotlin靜態方法和變量
- 11. 帶指針鑄造的局部變量
- 12. PHP - 來自變量的鑄造類型
- 13. 將lambda存儲在kotlin中的一個變量中
- 14. Theano SET_VALUE用於鑄造共享變量
- 15. ID變量和鑄造可可
- 16. 首先鑄造變量或保存?
- 17. 類型鑄造對象類型變量
- 18. 環境變量類型鑄造
- 19. 使用變量類型鑄造
- 20. 使用變量鑄造C結構
- 21. 協變鑄造解決方案所需
- 22. 鑄造錯誤變更
- 23. 通用方法 - 參數類型鑄造
- 24. Lambda表達式鑄造
- 25. Python Eclipse類型鑄造智能感知解決方案
- 26. 如何使Kotlin中的方法參數變爲可變?
- 27. 訪問一個變量的變量被鑄造成int數組的指針python
- 28. C++類型鑄造指向常量變量的指針
- 29. 什麼是SQL中的非鑄造變量? [Prestashop]
- 30. 鑄造到變量中保存的類型
請添加代碼的文本形式剪斷,以便它可以複製-pasted。我不得不重新鍵入你的代碼來回答你的問題,這並不令人愉快。 – voddan