<@U6Y33L3ME> how to make this higher order lamda f...
# android
j
@edwardwongtl how to make this higher order lamda funtion without pass to constructor { (view, item) -> ... // binding } but i want to pass this in object but not in that constructor so i can use like this adapter.onItemClick{position-> }
m
Easy just override method
Copy code
adapter.onItemClick() {
onItemClick?.invoke()
}
Add definition:
Copy code
var onItemClick: ((Item) -> Unit)? = null
And override this property from outside
e
That's what I'm going to answer as well
But I would move to a field in adapter, and
fun onItemClick(action: Int -> Unit) { listener = action }
m
var onItemClick is property inside adapter, yeah
j
adapter.onItemClick() { onItemClick?.invoke() } where to add this and how to assign that value?
how to use this in my attach file?
e
Copy code
class Adapter(...): ... {
var listener: (Int -> Unit)? = null

fun onItemClick(action: Int -> Unit) { listener = action }

fun onBindViewHolder(...) {
...
holder.itemview.setOnClickerListener { listener?.invoke(holder.getAdapterPosition() }
}
j
adapterFeed?.apply { }
in this how to get that onItemClick?
e
I don't understand what you want to ask
j
adapterFeed = AdapterFeed(instanceActBase()!!, getQArrayList) rcvFeed.apply { layoutManager = LinearLayoutManager(instanceActBase(), LinearLayoutManager.VERTICAL, false) adapter = adapterFeed } adapterFeed?.apply { }
this is my code but how to use onItemCLick ?
m
Copy code
adapterFeed.onItemClick = {item -> ….}
just pass lambda inside adapter and you’re good to go
j
thank you @miszmaniac
m
and as @edwardwongtl said don’t pass position inside lambda resolve item before using
Copy code
getItem(position)
j
what happen if i want to pass position to that lamda?
e
You should use
holder.getAdapterPosition()
to get the actual position
m
item might change before you finish this lambda
j
ok
please explain how to work that while using position?
thanks for you co-operate @edwardwongtl and @miszmaniac
m
np! Have fun!
j
from how long you have to use kotlin ?
m
2 years - firstly rewritten all tests in app, and later just migrated to kolin as base lang
j
nice
e
About half year for me
j
nice i just stated with kotlin and make 1 project