https://kotlinlang.org logo
#android
Title
# android
j

jayant

03/13/2018, 9:31 AM
@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

miszmaniac

03/13/2018, 9:35 AM
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

edwardwongtl

03/13/2018, 9:36 AM
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

miszmaniac

03/13/2018, 9:37 AM
var onItemClick is property inside adapter, yeah
j

jayant

03/13/2018, 9:37 AM
adapter.onItemClick() { onItemClick?.invoke() } where to add this and how to assign that value?
how to use this in my attach file?
e

edwardwongtl

03/13/2018, 9:41 AM
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

jayant

03/13/2018, 9:42 AM
adapterFeed?.apply { }
in this how to get that onItemClick?
e

edwardwongtl

03/13/2018, 9:45 AM
I don't understand what you want to ask
j

jayant

03/13/2018, 9:45 AM
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

miszmaniac

03/13/2018, 9:46 AM
Copy code
adapterFeed.onItemClick = {item -> ….}
just pass lambda inside adapter and you’re good to go
j

jayant

03/13/2018, 9:46 AM
thank you @miszmaniac
m

miszmaniac

03/13/2018, 9:47 AM
and as @edwardwongtl said don’t pass position inside lambda resolve item before using
Copy code
getItem(position)
j

jayant

03/13/2018, 9:47 AM
what happen if i want to pass position to that lamda?
e

edwardwongtl

03/13/2018, 9:47 AM
You should use
holder.getAdapterPosition()
to get the actual position
m

miszmaniac

03/13/2018, 9:48 AM
item might change before you finish this lambda
j

jayant

03/13/2018, 9:49 AM
ok
please explain how to work that while using position?
thanks for you co-operate @edwardwongtl and @miszmaniac
m

miszmaniac

03/13/2018, 10:07 AM
np! Have fun!
j

jayant

03/13/2018, 10:08 AM
from how long you have to use kotlin ?
m

miszmaniac

03/13/2018, 10:09 AM
2 years - firstly rewritten all tests in app, and later just migrated to kolin as base lang
j

jayant

03/13/2018, 10:10 AM
nice
e

edwardwongtl

03/13/2018, 10:11 AM
About half year for me
j

jayant

03/13/2018, 10:13 AM
nice i just stated with kotlin and make 1 project