Android Data Binding pass arguments to onClick method

后端 未结 2 1317
天命终不由人
天命终不由人 2020-12-07 19:56

Is it possible to pass custom arguments to onClick method using the Data Binding Library? I have my layout xml file where I need to use the onClickListener:

2条回答
  •  甜味超标
    2020-12-07 20:35

    You can use a lambda expressions and pass the view in as a parameter.

     android:onClick="@{() -> callback.onCategoryClick(viewModel)}"
    

    If you need the view, you can pass that as well with:

     android:onClick="@{(view) -> callback.onCategoryClick(view, viewModel)}"
    

提交回复
热议问题