[英]Android two way data binding with onclick() not working
I am trying to implement android two way databinding in my code.我正在尝试在我的代码中实现 android 双向数据绑定。 I am not able get onclick event for a textview which I have used for two way data binding for its text.我无法获得文本视图的 onclick 事件,我已将其用于文本的两种方式数据绑定。
ps:附言:
<TextView
android:id="@+id/likes"
android:clickable="true"
android:onClick="@{clickHandler::onClickEvents}"
android:text='@=
{com.coffeeshots.app.foodieapp.utils.Converter.convertIntToString(food.likes)}'
</TextView>
` `
<data>
<variable
name="food"
type="com.coffeeshots.app.foodieapp.model.Food"/>
<import type="com.coffeeshots.app.foodieapp.utils.Converter"/>
<variable
name="clickHandler"
type="com.coffeeshots.app.foodieapp.utils.ClickHandler"/>
</data>
public void onClickEvents(View view) {
switch(view.getId()){
case R.id.likes:
if(!likesGiven){
likesGiven = true;
binding.likes.setText("1");
binding.dislikes.setClickable(false);
}
break;
}
I think the problem is the way i inflate the layout, Can anyone help me here???我认为问题是我膨胀布局的方式,有人可以帮我吗???
LayoutInflater layoutInflater = getLayoutInflater();
binding= DataBindingUtil.inflate(layoutInflater,R.layout.activity_add_food,null,false);
setContentView(binding.getRoot());
Try this:试试这个:
android:onClick="@{(view)-> clickHandler.onClickEvents(view)}"
Yes, your layout inflating is wrong.是的,您的布局膨胀是错误的。 Do like this:这样做:
ActivityAddFoodBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_add_food);
binding.setClickHandler(new ClickHandler());
You can also find the related official example here: https://developer.android.com/topic/libraries/data-binding/expressions#binding_data您还可以在这里找到相关的官方示例: https ://developer.android.com/topic/libraries/data-binding/expressions#binding_data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.