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

Francesco megna

07/06/2018, 8:17 AM
The best way would be to pass a function from the layout parent and in the layout of button i write “onClick=“@{onClickGenericMethod}“, but if i want to pass paramater this implementation not work
n

nikolay

07/06/2018, 8:20 AM
but can’t you do
android:onClick="@{() -> onClickGenericMethod(someParameter)}"
?
f

Francesco megna

07/06/2018, 8:25 AM
Maybe yes, the first question is which type i specify in the layout (<variable name=“onClickGenericMethod” type=“???“), the second question is “if i have i need to pass different paramater to the function called in the same button layout?
n

nikolay

07/06/2018, 8:29 AM
sorry, I don’t really get your question. In my case I have
ViewModel
from which I’m using the click listener
f

Francesco megna

07/06/2018, 8:56 AM
Don’t worry, thx, maybe i will post the question on stackoverflow with more info and code, i link you the question if you are curious
n

nikolay

07/06/2018, 8:59 AM
yeah, would be nice to see more code
just to get more context
f

Francesco megna

07/06/2018, 9:11 AM
sure
g

gildor

07/06/2018, 9:11 AM
You cannot pass onClickGenericMethod just as “method reference”
but you can pass it some interface with a single method and invoke this method
so instead
Copy code
onClickGenericMethod(someParameter)
You can have something like
Copy code
onClickGenericMethod.run(someParameter)
f

Francesco megna

07/06/2018, 10:47 AM
Yes with interface i can but if I want to pass one parameter to the first button and different parameter to the second?
My intent is to have Button component like React Button component, with data binding
Or maybe with .run you mean to pass the onClickGenericMethod from the parent layout?
g

gildor

07/06/2018, 10:50 AM
As I understand it work if your method and interface have compatible types, the same works for BindingAdapters
If you have simple sample project with this, I can try to show this approach there
f

Francesco megna

07/06/2018, 10:53 AM
My actual project is for my company, but I can create a sample project in this weekend, I’m really curious of your tips
U can see the button layout used in in main_activity, the limitation is the method in the handlers
g

gildor

07/09/2018, 1:24 AM
Looks like your project contains submodule
databindingexample
but no sources or url or this submodule
I changes one of my own samples for your case Please take a look on branch reusable-binding https://github.com/gildor/kotlin-android-sample/tree/reusable-binding
Also, one more thing. I use
android.view.View.OnClickListener
on button.xml, but this interface is not special, you can use any SAM interface with required signature, databinding allow you to use any method or interface with compatible signature, it’s how custom listeners for BindingAdapter works, binding compiler just check listener signature, not a type of used interface
f

Francesco megna

07/09/2018, 3:10 PM
Thanks for the answer, when I’m at home I look your samples
2 Views