The best way would be to pass a function from the ...
# android
f
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
but can’t you do
android:onClick="@{() -> onClickGenericMethod(someParameter)}"
?
f
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
sorry, I don’t really get your question. In my case I have
ViewModel
from which I’m using the click listener
f
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
yeah, would be nice to see more code
just to get more context
f
sure
g
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
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
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
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
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
Thanks for the answer, when I’m at home I look your samples