Colton Idle
11/06/2019, 4:15 AMinterface SaveListener {
fun onSave(name: String)
}
MY ISSUE: When I try to create the listener, it makes me write out the whole thing. I thought there was a way around this in Kotlin? Probably missing something basic.
So I have to write this
myController.listener = object : SaveListener {
override fun onSave(name: String) {
//do thing
}
}
But with kotlin I thought I'd be able to do this
myController.listener = SaveListener { //do thing }
Matthieu Esnault
11/06/2019, 4:44 AMAdam Powell
11/06/2019, 5:07 AM(String) -> Unit
as the type for something like your example instead of declaring an interface for ittjohnn
11/06/2019, 5:43 AMVishnu Haridas
11/06/2019, 6:11 AMRecyclerView
adapters.
// pseudo-code
class ProductsAdapter(List): Adapter() {
var onItemClick: ((Item) -> Unit)? = null
var onAddToCart: ((Item) -> Unit)? = null
var onBuyNow: ((Item) -> Unit)? = null
var onWishList: ((Item) -> Unit)? = null
...// other methods,
fun onBind(item){
val view = inflate(...)
view.addOnClickListener {
onItemClick?.invoke(item)
}
view.button_buy_now.addOnClickListener {
onBuyNow?.invoke(item)
}
}
...and later when creating the Adapter,
val adapter = ProductsAdapter(myProducts)
adapter.onAddToCart = { item ->
// process the item
}
adapter.onBuyNow = { item ->
// proceed to buy
}
ibraiz_teamo
11/06/2019, 8:37 AMVishnu Haridas
11/06/2019, 8:40 AMibraiz_teamo
11/06/2019, 8:41 AMColton Idle
11/06/2019, 9:14 AMVishnu Haridas
11/06/2019, 9:34 AMColton Idle
11/06/2019, 4:57 PM