Java Android 数据绑定将参数传递给 onClick 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37105066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Data Binding pass arguments to onClick method
提问by kolodach
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:
是否可以onClick
使用数据绑定库将自定义参数传递给方法?我有我需要使用 onClickListener 的布局 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<layout ...>
<data>
<variable
name="viewModel"
type="com.productivity.tiktak.ui.tracker.viewModel.CategoryViewModel"/>
<variable
name="callback"
type="com.productivity.tiktak.ui.tracker.TrackerAdapter"/>
</data>
<android.support.v7.widget.CardView
android:onClick="@{callback.onCategoryClick(viewModel)}"
...
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- ... Some stuff -->
</android.support.v7.widget.CardView>
</layout>
and I a have my click handler code here:
我在这里有我的点击处理程序代码:
public void onCategoryClick(View view, CategoryViewModel categoryViewModel)
{
//handler code...
}
Is it possible to pass my CategoryViewModel object from xml to click handler?
是否可以将我的 CategoryViewModel 对象从 xml 传递到单击处理程序?
采纳答案by yigit
You can use a lambda expressions and pass the view in as a parameter.
您可以使用 lambda 表达式并将视图作为参数传递。
android:onClick="@{() -> callback.onCategoryClick(viewModel)}"
If you need the view, you can pass that as well with:
如果您需要视图,您也可以通过以下方式传递:
android:onClick="@{(view) -> callback.onCategoryClick(view, viewModel)}"
回答by Khemraj
Multiple Ways:
多种方式:
Solution 1 (When you have Presenter)
解决方案 1(当您有演示者时)
You have a presenter or handler class like below
您有一个如下所示的演示者或处理者类
public class Presenter {
public void onSaveClick(View view, Task task){}
}
Now take variable of type Presenter
in your layout. and set click like below.
现在Presenter
在您的布局中使用类型变量。并设置点击如下。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="task" type="com.android.example.Task" />
<variable name="presenter" type="com.android.example.Presenter" />
</data>
<LinearLayout ...>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{(view) -> presenter.onSaveClick(view, task)}"
/>
</LinearLayout>
</layout>
You can add any more arguments in this.
您可以在其中添加更多参数。
Please note that, you have to set these data variables. Like
请注意,您必须设置这些数据变量。喜欢
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setTask(task);
binding.setPresenter(presenter);
Solution 2 (When you don't have Presenter)
解决方案 2(当您没有 Presenter 时)
Create a variable type of your Activity / Fragment
创建您的活动/片段的变量类型
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="task" type="com.android.example.Task" />
<variable name="activity" type="com.example.MainActivity" />
</data>
<LinearLayout ...>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{(view) -> activity.onSaveClick(view, task)}"
/>
</LinearLayout>
</layout>
and make method in your activity which you want to call from binding layout. and don't forget to callsetActivity(YourActivity.this)
from Activity.
并在您的活动中创建要从绑定布局调用的方法。并且不要忘记setActivity(YourActivity.this)
从 Activity调用。
public class MainActivity extends AppCompatActivity {
public void onSaveClick(View view, Task task) {
System.out.println("MainActivity.onSaveClick");
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setActivity(this);
binding.setTask(task);
}
}
Further reading Android DataBinding documentation
There are many other ways to set click, check this answer.
还有许多其他设置点击的方法,请查看此答案。
回答by rakesh rajput
checkout mention link for onClick normal and custom click there we can we View.OnClickListener https://github.com/rakeshrajput379/ClickEventMVVM
结帐提及 onClick 正常和自定义点击链接,我们可以查看.OnClickListener https://github.com/rakeshrajput379/ClickEventMVVM